Solved

Tree .... data structure in C++

Posted on 2003-12-08
3
706 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 80 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

734 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