473,249 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,249 software developers and data experts.

Initializing: class within structure

I expect this has been covered somewhere, but I can't find it. Given:

class A { public:
A() {...does stuff...}
};

struct S {
A a;
};

If I create an instance of S, is A's initializer guaranteed to run? Put
differently, is the compiler committed to search down through many
levels to find things with constructors? (I know it will, of course, if
S is a class.)

I've run across verbiage that claims that struct is the same as class
with an initial public: specification, but I thought I recalled that
structs and classes differed more than that. If they don't, if the
intent is to write a POD with public data, just a few simple
constructors and no use of virtual, does it simply make more sense to
use struct, to make that intention clearer?

Thanks.

Jul 11 '06 #1
5 2539
ScottM wrote:
I expect this has been covered somewhere, but I can't find it. Given:

class A { public:
A() {...does stuff...}
};

struct S {
A a;
};

If I create an instance of S, is A's initializer guaranteed to run?
Yes. And it is actually called "constructor", not "initializer".
Put differently, is the compiler committed to search down through many
levels to find things with constructors? (I know it will, of course,
if S is a class.)
Yes. There is no difference between 'class' and 'struct' WRT this.
I've run across verbiage that claims that struct is the same as class
with an initial public: specification, but I thought I recalled that
structs and classes differed more than that. If they don't,
They don't.
if the
intent is to write a POD with public data, just a few simple
constructors and no use of virtual, does it simply make more sense to
use struct, to make that intention clearer?
Sure. Of course it depends on who's interpreting your intentions.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 11 '06 #2

"ScottM" <sc***@mayo.nameskrev i meddelandet
news:11*********************@35g2000cwc.googlegrou ps.com...
>I expect this has been covered somewhere, but I can't find it. Given:

class A { public:
A() {...does stuff...}
};

struct S {
A a;
};

If I create an instance of S, is A's initializer guaranteed to run?
Put
differently, is the compiler committed to search down through many
levels to find things with constructors? (I know it will, of course,
if
S is a class.)
Yes, there is no difference. The compiler initializes each member,
which means initializing the member's members, and so on.
>
I've run across verbiage that claims that struct is the same as
class
with an initial public: specification, but I thought I recalled that
structs and classes differed more than that. If they don't, if the
intent is to write a POD with public data, just a few simple
constructors and no use of virtual, does it simply make more sense
to
use struct, to make that intention clearer?
Using a struct as a collection of some related data, is a good idea.
Adding constructors is not, if you want to keep it a POD. PODs are C
style data, which doesn't have constructors.
Bo Persson
Jul 11 '06 #3
ScottM wrote:
I expect this has been covered somewhere, but I can't find it. Given:

class A { public:
A() {...does stuff...}
};

struct S {
A a;
};

If I create an instance of S, is A's initializer guaranteed to run? Put
differently, is the compiler committed to search down through many
levels to find things with constructors? (I know it will, of course, if
S is a class.)
If you don't write a constructor for S, then creating an instance of S
will call A's default constructor. If A doesn't have a default
constructor, then I think you'll get a compiler error. In that case
you will have to write a constructor for S that calls A's constructor.
So, the answer to your question is yes. For structs and classes, the
compiler will construct all "sub-objects" that it can figure out how to
create.
I've run across verbiage that claims that struct is the same as class
with an initial public: specification, but I thought I recalled that
structs and classes differed more than that. If they don't, if the
intent is to write a POD with public data, just a few simple
constructors and no use of virtual, does it simply make more sense to
use struct, to make that intention clearer?
I found a good answer to this in the FAQ:
http://www.parashift.com/c++-faq-lit...s.html#faq-7.8

While you're at it, I recommend reading the whole thing. Lots of good
stuff in there.

Kristo

Jul 11 '06 #4
In message <11*********************@35g2000cwc.googlegroups.c om>, ScottM
<sc***@mayo.namewrites
>I expect this has been covered somewhere, but I can't find it. Given:

class A { public:
A() {...does stuff...}
};

struct S {
A a;
};

If I create an instance of S, is A's initializer guaranteed to run? Put
differently, is the compiler committed to search down through many
levels to find things with constructors? (I know it will, of course, if
S is a class.)
Yes.
>
I've run across verbiage that claims that struct is the same as class
with an initial public: specification,
And default public inheritance.
>but I thought I recalled that
structs and classes differed more than that.
You're possibly thinking of another, slightly related, language with C
in its name.
>If they don't, if the
intent is to write a POD with public data, just a few simple
constructors
If it has constructors it isn't a POD!
>and no use of virtual, does it simply make more sense to
use struct, to make that intention clearer?
Yes. It's a common convention in some circles -- but if you're going to
use it consistently as a documentation tool you need to spell out
exactly what your chosen rules will be.

--
Richard Herring
Jul 11 '06 #5
Got it. Thanks for all replies.

Jul 11 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Doug C via .NET 247 | last post by:
Using C#... I am pulling shared memory in to my app that is in the form of apredefined structure. I have arrays in 2 sub-structures. Onearray is an array of another predefined structure, and the...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
1
by: Keith Rebello | last post by:
I have written a class to draw dimension lines (like you see in architectural drawings). I intend to use this class in engineering programs that have graphical output. The class has various...
2
by: Steve | last post by:
I have defined a structure of; Private Structure Menu Private MenuID as string Private Label as string End Structure ....and I would like to create and initialize an array of type "Menu". I...
0
by: seek help | last post by:
Hello, IDE: VS .NET 2003. Problem: Mangled bits of gc class embedded within native C++ class. Description: I have a gc class, BoxedInfo. This wraps a Value type structure, NotiInfo....
11
by: sg71.cherub | last post by:
Hi All, I have encapsulate CvMat of OpenCV into my own matrix class as the following: class CVMatrix { //== Fields private: unsigned m_Width;
12
by: Mik0b0 | last post by:
Hallo. Let's say, there is a structure struct struct10{ int a1; int a2; int a3; int a4; }count={ {10,20,30,40}, {50,60,70,80}
1
SamKL
by: SamKL | last post by:
Hey, I'm no expert on PHP, and I have somewhat of an understanding of object oriented code. Anyway, getting right to the problem. I'm using PHP4, so base it off of that. Basically I have 2...
2
by: Martin Payne | last post by:
I am trying to initialise a structure with random values for all its fields. It is a large structure so I do not want to do it for each element in turn (it would take ages). Please note my...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.