Why use a private struct?

S

Svenn Are Bjerkem

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,
 
J

Jim Langston

Svenn Are Bjerkem said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top