Why use a private struct?

Discussion in 'C++' started by Svenn Are Bjerkem, Mar 21, 2007.

  1. Hi Forum,

    I am not very much experienced with c++, but I have unfortunately
    programmed a lot of c and have a problem understanding the purpose of
    defining a struct in the private part of a class. I am looking at the
    example code for Interviews in the Qt-4 example code, which
    unfortunately hasn't been documented yet.

    http://doc.trolltech.com/4.2/demos-interview.html

    The part of the code in model.h I don't really understand look like
    this:

    private:

    struct Node
    {
    Node(Node *parent = 0) : parent(parent), children(0) {}
    ~Node() { delete children; }
    Node *parent;
    QVector<Node> *children;
    };

    Node *node(int row, Node *parent) const;
    Node *parent(Node *child) const;
    int row(Node *node) const;

    int rc, cc;
    QVector<Node> *tree;
    };

    I guess there is a rationale behind doing it this way, but I haven't
    found anything like this in the c++ book I have, and a search on
    google mostly explain the difference between struct and class and not
    how to use a struct like this example. Wouldn't normally Node be
    defined as a class on its own? What is the advantage of doing things
    this way? Was my google search too lax so that I missed a FAQ
    somewhere?

    Kind regards,
    --
    Svenn
    Svenn Are Bjerkem, Mar 21, 2007
    #1
    1. Advertising

  2. Svenn Are Bjerkem

    Jim Langston Guest

    "Svenn Are Bjerkem" <> wrote in message
    news:...
    > Hi Forum,
    >
    > I am not very much experienced with c++, but I have unfortunately
    > programmed a lot of c and have a problem understanding the purpose of
    > defining a struct in the private part of a class. I am looking at the
    > example code for Interviews in the Qt-4 example code, which
    > unfortunately hasn't been documented yet.
    >
    > http://doc.trolltech.com/4.2/demos-interview.html
    >
    > The part of the code in model.h I don't really understand look like
    > this:
    >
    > private:
    >
    > struct Node
    > {
    > Node(Node *parent = 0) : parent(parent), children(0) {}
    > ~Node() { delete children; }
    > Node *parent;
    > QVector<Node> *children;
    > };
    >
    > Node *node(int row, Node *parent) const;
    > Node *parent(Node *child) const;
    > int row(Node *node) const;
    >
    > int rc, cc;
    > QVector<Node> *tree;
    > };
    >
    > I guess there is a rationale behind doing it this way, but I haven't
    > found anything like this in the c++ book I have, and a search on
    > google mostly explain the difference between struct and class and not
    > how to use a struct like this example. Wouldn't normally Node be
    > defined as a class on its own? What is the advantage of doing things
    > this way? Was my google search too lax so that I missed a FAQ
    > somewhere?


    Without looking at the code in question but only at the snippet you show
    here, I would say because nothing outside the class needs, or should, know
    about Node. It is internal to the class and only the class uses it.

    Another rationality may be that Node is a fairly generic name. If it was
    taken out of the class private declaration it would have larger scope and
    may cause name conflicts.

    The question is, if only this class needs to know about Node and use it, why
    would you need or want to broaden it's scope needlessly?
    Jim Langston, Mar 21, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,353
    Chris Fogelklou
    Apr 20, 2004
  2. Jeremy Bowers
    Replies:
    3
    Views:
    501
    Steven Bethard
    Jan 24, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,756
    Smokey Grindel
    Dec 2, 2006
  4. Daniel Finnie
    Replies:
    3
    Views:
    174
    Logan Capaldo
    Dec 16, 2006
  5. Rie!
    Replies:
    3
    Views:
    120
Loading...

Share This Page