Is a created before b? 
July 22nd, 2005, 09:58 AM
| | | |
Given three classes:
class A
{
};
class B
{
};
class C
{
A a;
B b;
};
Is a for sure instantiated before b? If not, then is there any way to make
sure that? How to make sure a is deleted after b's deletion?
Thanks! | 
July 22nd, 2005, 09:58 AM
| | | | re: Is a created before b?
ikl wrote:[color=blue]
> Given three classes:
>
> class A
> {
> };
>
> class B
> {
> };
>
> class C
> {
> A a;
> B b;
> };
>
> Is a for sure instantiated before b?[/color]
Yes. Member variables are constructed in the order in which they appear
in the class definition.
--
Russell Hanneken rghanneken@pobox.com
Remove the 'g' from my address to send me mail. | 
July 22nd, 2005, 09:58 AM
| | | | re: Is a created before b?
ikl wrote:[color=blue]
> Given three classes:
>
> class A
> {
> };
>
> class B
> {
> };
>
> class C
> {
> A a;
> B b;
> };
>
> Is a for sure instantiated before b?[/color]
Yes.
[color=blue]
> How to make sure a is deleted after b's deletion?[/color]
Since neither "a" nor "b" is allocated dynamically, I think you mean
"destructed," not "deleted." The answer is that "b" will be destructed
first unless you jump through some pretty contorted hoops. Is there a
particular situation in which you find the default behavior unsuitable? | 
July 22nd, 2005, 09:58 AM
| | | | re: Is a created before b?
ikl wrote:[color=blue]
> Given three classes:
>
> class A
> {
> };
>
> class B
> {
> };
>
> class C
> {
> A a;
> B b;
> };
>
> Is a for sure instantiated before b?[/color]
Yes. Member variables are constructed in the order in which they appear
in the class definition.
--
Russell Hanneken rghanneken@pobox.com
Remove the 'g' from my address to send me mail. | 
July 22nd, 2005, 09:58 AM
| | | | re: Is a created before b?
ikl wrote:[color=blue]
> Given three classes:
>
> class A
> {
> };
>
> class B
> {
> };
>
> class C
> {
> A a;
> B b;
> };
>
> Is a for sure instantiated before b?[/color]
Yes.
[color=blue]
> How to make sure a is deleted after b's deletion?[/color]
Since neither "a" nor "b" is allocated dynamically, I think you mean
"destructed," not "deleted." The answer is that "b" will be destructed
first unless you jump through some pretty contorted hoops. Is there a
particular situation in which you find the default behavior unsuitable? | 
July 22nd, 2005, 09:58 AM
| | | | re: Is a created before b?
Since the clas A instance is specified prior to the class B instance it
will be guaranteed to be constructed before the class B instance. The
destructors are then guaranteed to be run in the reverse order. This
is regardless of any way constructors for such classes A and B may
be specified in initializer lists for class C.
Regards,
Neil
On Mon, 5 Apr 2004, ikl wrote:
[color=blue]
> Given three classes:
>
> class A
> {
> };
>
> class B
> {
> };
>
> class C
> {
> A a;
> B b;
> };[/color]
int main() {
C c;
}
[color=blue]
>
> Is a for sure instantiated before b? If not, then is there any way to make
> sure that? How to make sure a is deleted after b's deletion?
>
> Thanks![/color] | 
July 22nd, 2005, 09:58 AM
| | | | re: Is a created before b?
Since the clas A instance is specified prior to the class B instance it
will be guaranteed to be constructed before the class B instance. The
destructors are then guaranteed to be run in the reverse order. This
is regardless of any way constructors for such classes A and B may
be specified in initializer lists for class C.
Regards,
Neil
On Mon, 5 Apr 2004, ikl wrote:
[color=blue]
> Given three classes:
>
> class A
> {
> };
>
> class B
> {
> };
>
> class C
> {
> A a;
> B b;
> };[/color]
int main() {
C c;
}
[color=blue]
>
> Is a for sure instantiated before b? If not, then is there any way to make
> sure that? How to make sure a is deleted after b's deletion?
>
> Thanks![/color] | 
July 22nd, 2005, 09:58 AM
| | | | re: Is a created before b?
Jeff Schwab wrote:
[color=blue][color=green]
>> How to make sure a is deleted after b's deletion?[/color]
>
> Since neither "a" nor "b" is allocated dynamically, I think you mean
> "destructed," not "deleted."[/color]
I think the word is "destroyed" ;-)
[color=blue]
> The answer is that "b" will be destructed first unless you jump
> through some pretty contorted hoops. Is there a particular situation
> in which you find the default behavior unsuitable?[/color]
The OP actually wanted that behaviour. | 
July 22nd, 2005, 09:58 AM
| | | | re: Is a created before b?
Jeff Schwab wrote:
[color=blue][color=green]
>> How to make sure a is deleted after b's deletion?[/color]
>
> Since neither "a" nor "b" is allocated dynamically, I think you mean
> "destructed," not "deleted."[/color]
I think the word is "destroyed" ;-)
[color=blue]
> The answer is that "b" will be destructed first unless you jump
> through some pretty contorted hoops. Is there a particular situation
> in which you find the default behavior unsuitable?[/color]
The OP actually wanted that behaviour. | 
July 22nd, 2005, 09:59 AM
| | | | re: Is a created before b?
Rolf Magnus wrote:[color=blue]
> Jeff Schwab wrote:
>
>[color=green][color=darkred]
>>>How to make sure a is deleted after b's deletion?[/color]
>>
>>Since neither "a" nor "b" is allocated dynamically, I think you mean
>>"destructed," not "deleted."[/color]
>
>
> I think the word is "destroyed" ;-)[/color]
I think both terms are fine. "Destruct" is what an object does when its
"destructor" is called, the process being known as "destruction."
"destroy" happens to be the name of the method in the standard
allocators that causes the destruction of an object, so I prefer to say
an object whose destructor is invoked implicity by the run-time
environment is "destructed," rather than "destroyed."
[color=blue][color=green]
>>The answer is that "b" will be destructed first unless you jump
>>through some pretty contorted hoops. Is there a particular situation
>>in which you find the default behavior unsuitable?[/color]
>
>
> The OP actually wanted that behaviour.[/color]
Yes, I see (upon re-reading the OP) that you're right. What I said is
true, it's just not very useful. :) | 
July 22nd, 2005, 09:59 AM
| | | | re: Is a created before b?
Rolf Magnus wrote:[color=blue]
> Jeff Schwab wrote:
>
>[color=green][color=darkred]
>>>How to make sure a is deleted after b's deletion?[/color]
>>
>>Since neither "a" nor "b" is allocated dynamically, I think you mean
>>"destructed," not "deleted."[/color]
>
>
> I think the word is "destroyed" ;-)[/color]
I think both terms are fine. "Destruct" is what an object does when its
"destructor" is called, the process being known as "destruction."
"destroy" happens to be the name of the method in the standard
allocators that causes the destruction of an object, so I prefer to say
an object whose destructor is invoked implicity by the run-time
environment is "destructed," rather than "destroyed."
[color=blue][color=green]
>>The answer is that "b" will be destructed first unless you jump
>>through some pretty contorted hoops. Is there a particular situation
>>in which you find the default behavior unsuitable?[/color]
>
>
> The OP actually wanted that behaviour.[/color]
Yes, I see (upon re-reading the OP) that you're right. What I said is
true, it's just not very useful. :) | 
July 22nd, 2005, 10:01 AM
| | | | re: Is a created before b?
Neil Zanella <nzanella@cs.mun.ca> wrote in message
news:Pine.LNX.4.44.0404050100170.11197-100000@garfield.cs.mun.ca...[color=blue]
>
> Since the clas A instance is specified prior to the class B instance it
> will be guaranteed to be constructed before the class B instance. The
> destructors are then guaranteed to be run in the reverse order. This
> is regardless of any way constructors for such classes A and B may
> be specified in initializer lists for class C.
>
> Regards,
>
> Neil
>
> On Mon, 5 Apr 2004, ikl wrote:
>[color=green]
> > Given three classes:
> >
> > class A
> > {
> > };
> >
> > class B
> > {
> > };
> >
> > class C
> > {
> > A a;
> > B b;
> > };[/color]
>
> int main() {
> C c;
> }
>[color=green]
> >
> > Is a for sure instantiated before b? If not, then is there any way to[/color][/color]
make[color=blue][color=green]
> > sure that? How to make sure a is deleted after b's deletion?
> >
> > Thanks![/color]
>[/color]
Thanks to you all! This is not a question to me any more.
Is there any difference if a and b in any order on initializer lists for
class C?
Does an initializer list always go with a definition of a constructor not
declaration?
If there is a method in C that makes the class C like,
class C
{
A a;
B b;
public:
void method1(A& a1, B& b1);
};
When method1() is invoked, is it always safe to say that a and b have been
created already? | 
July 22nd, 2005, 10:01 AM
| | | | re: Is a created before b?
Neil Zanella <nzanella@cs.mun.ca> wrote in message
news:Pine.LNX.4.44.0404050100170.11197-100000@garfield.cs.mun.ca...[color=blue]
>
> Since the clas A instance is specified prior to the class B instance it
> will be guaranteed to be constructed before the class B instance. The
> destructors are then guaranteed to be run in the reverse order. This
> is regardless of any way constructors for such classes A and B may
> be specified in initializer lists for class C.
>
> Regards,
>
> Neil
>
> On Mon, 5 Apr 2004, ikl wrote:
>[color=green]
> > Given three classes:
> >
> > class A
> > {
> > };
> >
> > class B
> > {
> > };
> >
> > class C
> > {
> > A a;
> > B b;
> > };[/color]
>
> int main() {
> C c;
> }
>[color=green]
> >
> > Is a for sure instantiated before b? If not, then is there any way to[/color][/color]
make[color=blue][color=green]
> > sure that? How to make sure a is deleted after b's deletion?
> >
> > Thanks![/color]
>[/color]
Thanks to you all! This is not a question to me any more.
Is there any difference if a and b in any order on initializer lists for
class C?
Does an initializer list always go with a definition of a constructor not
declaration?
If there is a method in C that makes the class C like,
class C
{
A a;
B b;
public:
void method1(A& a1, B& b1);
};
When method1() is invoked, is it always safe to say that a and b have been
created already? | 
July 22nd, 2005, 10:01 AM
| | | | re: Is a created before b?
"ikl" <ikl72@dsp.com> wrote in message
news:RHscc.26056$vo5.826781@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
> Neil Zanella <nzanella@cs.mun.ca> wrote in message
> news:Pine.LNX.4.44.0404050100170.11197-100000@garfield.cs.mun.ca...[color=green]
> >
> > Since the clas A instance is specified prior to the class B instance it
> > will be guaranteed to be constructed before the class B instance. The
> > destructors are then guaranteed to be run in the reverse order. This
> > is regardless of any way constructors for such classes A and B may
> > be specified in initializer lists for class C.
> >
> > Regards,
> >
> > Neil
> >
> > On Mon, 5 Apr 2004, ikl wrote:
> >[color=darkred]
> > > Given three classes:
> > >
> > > class A
> > > {
> > > };
> > >
> > > class B
> > > {
> > > };
> > >
> > > class C
> > > {
> > > A a;
> > > B b;
> > > };[/color]
> >
> > int main() {
> > C c;
> > }
> >[color=darkred]
> > >
> > > Is a for sure instantiated before b? If not, then is there any way to[/color][/color]
> make[color=green][color=darkred]
> > > sure that? How to make sure a is deleted after b's deletion?
> > >
> > > Thanks![/color]
> >[/color]
> Thanks to you all! This is not a question to me any more.
>
> Is there any difference if a and b in any order on initializer lists for
> class C?[/color]
The order of initialization is that of the
order of the member definitions. The expressions
in the initializer list can be in any order, and
do not affect initialization order.
[color=blue]
>
> Does an initializer list always go with a definition of a constructor not
> declaration?[/color]
Correct.
[color=blue]
>
> If there is a method in C that makes the class C like,
> class C
> {
> A a;
> B b;
> public:
> void method1(A& a1, B& b1);
> };
>
> When method1() is invoked, is it always safe to say that a and b have been
> created already?[/color]
Yes. Once a constructor has run (whether it's user-defined
or not), the object exists, and member functions can be
invoked against it.
-Mike | 
July 22nd, 2005, 10:01 AM
| | | | re: Is a created before b?
"ikl" <ikl72@dsp.com> wrote in message
news:RHscc.26056$vo5.826781@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
> Neil Zanella <nzanella@cs.mun.ca> wrote in message
> news:Pine.LNX.4.44.0404050100170.11197-100000@garfield.cs.mun.ca...[color=green]
> >
> > Since the clas A instance is specified prior to the class B instance it
> > will be guaranteed to be constructed before the class B instance. The
> > destructors are then guaranteed to be run in the reverse order. This
> > is regardless of any way constructors for such classes A and B may
> > be specified in initializer lists for class C.
> >
> > Regards,
> >
> > Neil
> >
> > On Mon, 5 Apr 2004, ikl wrote:
> >[color=darkred]
> > > Given three classes:
> > >
> > > class A
> > > {
> > > };
> > >
> > > class B
> > > {
> > > };
> > >
> > > class C
> > > {
> > > A a;
> > > B b;
> > > };[/color]
> >
> > int main() {
> > C c;
> > }
> >[color=darkred]
> > >
> > > Is a for sure instantiated before b? If not, then is there any way to[/color][/color]
> make[color=green][color=darkred]
> > > sure that? How to make sure a is deleted after b's deletion?
> > >
> > > Thanks![/color]
> >[/color]
> Thanks to you all! This is not a question to me any more.
>
> Is there any difference if a and b in any order on initializer lists for
> class C?[/color]
The order of initialization is that of the
order of the member definitions. The expressions
in the initializer list can be in any order, and
do not affect initialization order.
[color=blue]
>
> Does an initializer list always go with a definition of a constructor not
> declaration?[/color]
Correct.
[color=blue]
>
> If there is a method in C that makes the class C like,
> class C
> {
> A a;
> B b;
> public:
> void method1(A& a1, B& b1);
> };
>
> When method1() is invoked, is it always safe to say that a and b have been
> created already?[/color]
Yes. Once a constructor has run (whether it's user-defined
or not), the object exists, and member functions can be
invoked against it.
-Mike | 
July 22nd, 2005, 10:03 AM
| | | | re: Is a created before b?
Jeff Schwab wrote:[color=blue]
> Rolf Magnus wrote:
>[color=green]
>> Jeff Schwab wrote:
>>
>>[color=darkred]
>>>> How to make sure a is deleted after b's deletion?
>>>
>>>
>>> Since neither "a" nor "b" is allocated dynamically, I think you mean
>>> "destructed," not "deleted."[/color]
>>
>>
>>
>> I think the word is "destroyed" ;-)[/color]
>
>
> I think both terms are fine. "Destruct" is what an object does when its
> "destructor" is called, the process being known as "destruction."[/color]
I don't think Rolf was correcting your terms, just your
grammar/spelling: "destructed" is not in the English dictionary.
Destruct - to Destroy. "It has been destroyed - I see only destruction."
--
Ben Measures
Software programming, Internet design/programming, Gaming freak. http://ben.measures.org.uk - when I find time | 
July 22nd, 2005, 10:03 AM
| | | | re: Is a created before b?
Jeff Schwab wrote:[color=blue]
> Rolf Magnus wrote:
>[color=green]
>> Jeff Schwab wrote:
>>
>>[color=darkred]
>>>> How to make sure a is deleted after b's deletion?
>>>
>>>
>>> Since neither "a" nor "b" is allocated dynamically, I think you mean
>>> "destructed," not "deleted."[/color]
>>
>>
>>
>> I think the word is "destroyed" ;-)[/color]
>
>
> I think both terms are fine. "Destruct" is what an object does when its
> "destructor" is called, the process being known as "destruction."[/color]
I don't think Rolf was correcting your terms, just your
grammar/spelling: "destructed" is not in the English dictionary.
Destruct - to Destroy. "It has been destroyed - I see only destruction."
--
Ben Measures
Software programming, Internet design/programming, Gaming freak. http://ben.measures.org.uk - when I find time |  | | | | /bytes/about
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 225,662 network members.
|