473,412 Members | 1,944 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,412 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 2557
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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,...
0
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...
0
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...

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.