Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Tree .... data structure in C++

Posted on 2003-12-08
3
Medium Priority
?
709 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
  • 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

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

824 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