Connecting Tech Pros Worldwide Help | Site Map

Is a created before b?

  #1  
Old July 22nd, 2005, 09:58 AM
ikl
Guest
 
Posts: n/a
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!




  #2  
Old July 22nd, 2005, 09:58 AM
Russell Hanneken
Guest
 
Posts: n/a

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.
  #3  
Old July 22nd, 2005, 09:58 AM
Jeff Schwab
Guest
 
Posts: n/a

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?
  #4  
Old July 22nd, 2005, 09:58 AM
Russell Hanneken
Guest
 
Posts: n/a

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.
  #5  
Old July 22nd, 2005, 09:58 AM
Jeff Schwab
Guest
 
Posts: n/a

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?
  #6  
Old July 22nd, 2005, 09:58 AM
Neil Zanella
Guest
 
Posts: n/a

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]

  #7  
Old July 22nd, 2005, 09:58 AM
Neil Zanella
Guest
 
Posts: n/a

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]

  #8  
Old July 22nd, 2005, 09:58 AM
Rolf Magnus
Guest
 
Posts: n/a

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.

  #9  
Old July 22nd, 2005, 09:58 AM
Rolf Magnus
Guest
 
Posts: n/a

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.

  #10  
Old July 22nd, 2005, 09:59 AM
Jeff Schwab
Guest
 
Posts: n/a

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. :)
  #11  
Old July 22nd, 2005, 09:59 AM
Jeff Schwab
Guest
 
Posts: n/a

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. :)
  #12  
Old July 22nd, 2005, 10:01 AM
ikl
Guest
 
Posts: n/a

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?


  #13  
Old July 22nd, 2005, 10:01 AM
ikl
Guest
 
Posts: n/a

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?


  #14  
Old July 22nd, 2005, 10:01 AM
Mike Wahler
Guest
 
Posts: n/a

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


  #15  
Old July 22nd, 2005, 10:01 AM
Mike Wahler
Guest
 
Posts: n/a

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


  #16  
Old July 22nd, 2005, 10:03 AM
Ben Measures
Guest
 
Posts: n/a

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
  #17  
Old July 22nd, 2005, 10:03 AM
Ben Measures
Guest
 
Posts: n/a

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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a tab be created in form when a record is created or query is run? Rotorian answers 4 October 9th, 2009 03:00 AM
VBA no errors, but no table is created?? help? timber910 answers 0 June 3rd, 2009 05:55 PM
Create a new record in one table when a record is created in another? dstorms answers 1 November 6th, 2007 07:19 PM
How to override the level in which the SoapEnvelope is created. Dorit answers 0 February 8th, 2006 06:55 AM
what is teh event name for after a form is created and loaded jg answers 3 January 5th, 2006 01:15 AM