Initializing: class within structure
Question posted by: ScottM
(Guest)
on
July 11th, 2006 03:25 PM
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.
5
Answers Posted
ScottM wrote:
Quote:
Originally Posted by
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".
Quote:
Originally Posted by
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.
Quote:
Originally Posted by
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.
Quote:
Originally Posted by
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
"ScottM" <scott@mayo.nameskrev i meddelandet
news:1152627913.652846.13320@35g2000cwc.googlegrou ps.com...
Quote:
Originally Posted by
>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.
Quote:
Originally Posted by
>
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
ScottM wrote:
Quote:
Originally Posted by
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.
Quote:
Originally Posted by
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-li...ts.html#faq-7.8
While you're at it, I recommend reading the whole thing. Lots of good
stuff in there.
Kristo
In message <1152627913.652846.13320@35g2000cwc.googlegroups.co m>, ScottM
<scott@mayo.namewrites
Quote:
Originally Posted by
>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.
Quote:
Originally Posted by
>
>I've run across verbiage that claims that struct is the same as class
>with an initial public: specification,
And default public inheritance.
Quote:
Originally Posted by
>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.
Quote:
Originally Posted by
>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!
Quote:
Originally Posted by
>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
Got it. Thanks for all replies.
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 196,996 network members.
Top Community Contributors
|