By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,303 Members | 1,652 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,303 IT Pros & Developers. It's quick & easy.

Why use a private struct?

P: n/a
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

Mar 21 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Svenn Are Bjerkem" <sv***********@googlemail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
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?
Mar 21 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.