Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Tree .... data structure in C++

Posted on 2003-12-08
3
Medium Priority
?
708 Views
Last Modified: 2010-04-01
Hi Experts,

     I am currently trying to understand the implementation of the tree structure. The following is a tree class and I have several questions about it :
--------------------------------------------------------------------------------------------
class Tree
{   struct Node ;
    typedef string Type ;
    typedef list<Node*> List ;
    typedef List::iterator LIt ;
    List _nodes ;

  public :
    class Iterator ;     //line01, Q1
    Tree() ;
    Tree(const Tree&) ;
    Tree(const Type&) ;
    Tree(const Type&, const list<Tree*>&) ;
    ~Tree() ;
    Tree& operator=(const Tree& t) ;
    void clear() ;
    Iterator begin() ;
    Iterator end() ;
    friend class Iterator    //line02, Q1
    {  Tree* _tree ;
       LIt _lit ;
      public:
       Iterator() ;
       Iterator(const Iterator&) ;
       Iterator(Tree*, Node* = 0) ;
       Iterator(Tree*, LIt) ;
       void operator=(const Iterator& it) ;
       bool operator==(const Iterator& it) ;
       bool operator!=(const Iterator& it) ;
       Iterator& operator++() ;
       Iterator operator++(int) ;
       Type& operator*() const ;  // line03 ...Q2
       bool operator!() ;
       friend class Tree ;
    };
};
---------------------------------------------
  Q1: line01 and line02: It seems to me that the class "Iterator" were declared twice. The first time it was declared as an inner class; the second time it was declared as a friend class. This is a little bit wierd to me. Is that okay to declare something twice ? and is that okay to be an inner class and friend class at the same time ?

 Q2: line03: This is a more general question, not only in the tree structure. Sometimes I saw a declaration of a function looks like :"void fun1() const ;"  or "int fun2() const ;". What does the "const" mean here ???

   Thanks very much !

 meow......
0
Comment
Question by:meow00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9899768
Q1: line01 is a 'foward declaration', i.e. you are telling the compiler that you are going to use a typename that is defined later

Q2: The 'const' keyword means that this funcion is not manipulating any members of the class it belongs to - from the docs:

C++ Specific

Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called.

To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition. A constant member function cannot modify any data members or call any member functions that aren't constant.

END C++ Specific

Example

// Example of a constant member function
class Date
{
public:
   Date( int mn, int dy, int yr );
   int getMonth() const;       // A read-only function
   void setMonth( int mn );    // A write function;
                               //    cannot be const
private:
   int month;
};

int Date::getMonth() const
{
   return month;        // Doesn't modify anything
}
void Date::setMonth( int mn )
{
   month = mn;          // Modifies data member
}
0
 
LVL 1

Author Comment

by:meow00
ID: 9907524
Thanks for the answer.... just have a short question about Q1.
The "Iterator class" was really used between line01 and line02. Then why we need the line01 for forward declaration ? can we get rid of it ? Thanks.

meow....
0
 
LVL 86

Accepted Solution

by:
jkr earned 320 total points
ID: 9907553
If you'd move

    Iterator begin() ;
    Iterator end() ;

past the definition of 'Iterator', you can get rid of the fwd. declaration...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question