473,769 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why use a private struct?

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*ch ildren;
};

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

int rc, cc;
QVector<Node*tr ee;
};

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
1 10517
"Svenn Are Bjerkem" <sv***********@ googlemail.comw rote in message
news:11******** **************@ e65g2000hsc.goo glegroups.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*ch ildren;
};

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

int rc, cc;
QVector<Node*tr ee;
};

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2068
by: Richard Webb | last post by:
Hi all, I guess this is more of a design problem than a language problem, but I'm confused either way! I have a class and it has a private data member which is a struct. The size of the struct is what I would call relatively large (about 1Mb). I have written methods for this class so that the struct can be correctly filled with the corerct data and certain parts of the struct can be extracted. But the problem I face is that I now have...
3
5126
by: Mark | last post by:
I have a .cs file that contains a public class and a public struct in a single namespace. I decided I wanted the struct to only be accessible to the class, and not to the rest of my project. I changed the public struct to a private struct, but it gave me the following error ... why? What are my options? Thanks in advance! - Mark "Namespace elements cannot be explicitly declared as private, protected, or protected internal."
1
1120
by: Maxim Yegorushkin | last post by:
Code to reproduce the bug (Comeau refuses to compile the code, but VC has no complain about it): template<class T, class S> class M { private: struct m_tag; };
23
2516
by: Leif Gruenwoldt | last post by:
Is it possible to safely cast a struct to a class? The contents of both are the same size, however there is the issue of the class having virtual functions which make the size of the class slightly larger.
1
1875
by: swhite | last post by:
Hi, Sorry, not very clear from the title but wasn't sure what to call it. I was experimenting with the following in GCC 4.0.1 and didn't achieve what I expected. I was trying to keep the internal class private, to prevent creation, but also in the hope it might achieve something like when you use "class <foo>;" to early declare to allow pointer passing, but cannot access contents. Also I passed out a const iterator containing this...
74
16030
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
45
18901
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies of their elements? Why can't I change the element itself? class Program { private struct MyStruct
0
2712
by: zman77 | last post by:
Hi. I've managed to get the C code I need into a dll, and my C# program can access some functions. There are other functions though, and I don't know how to access them from C# because they involve a struct declared in the C code. The example I have been working from is the following: C-struct: struct STRUCT_DLL { int count_int; int* ints; }; C# code:
1
6663
by: eastlands | last post by:
I need to use an unmanaged c++ dll which uses structs that contain callbacks and also functions. I have included the appropriate c++ definitials and my c# translations below. I first defined the parameters as c# structs, but when I tried the call (to RtcInitialize) I got the following error: MarshalDirectiveException Method's type signature is not PInvoke compatible. I then changed the structs to classes but now I get the following...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7401
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.