Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Call constructor/destructor of base class

Reply
Thread Tools

Call constructor/destructor of base class

 
 
Fabian Müller
Guest
Posts: n/a
 
      07-06-2004
Hi all,

my question is as follows:

If have a class X and a class Y derived from X.

Constructor of X is X(param1, param2) .

Constructor of Y is Y(param1, ..., param4) .

How can I call the constructor/destructor of X from the
constructor/destructor of Y, before I the rest of the code of
constructor/destructor of Y.

Can I use the 'this' keyword. It gets compiled for the destructor but doen't
for the constructor. ( ((X*) this)->X( ... ) )

When calling the constructor of X from the constructor of Y, with X(...) it
gets compiled, but seems not to work, because I get segmentation errors at
runtime.

Many thanks to any advice.

Fabian Müller




 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-06-2004
"Fabian Müller" wrote:
>
> Hi all,
>
> my question is as follows:
>
> If have a class X and a class Y derived from X.
>
> Constructor of X is X(param1, param2) .
>
> Constructor of Y is Y(param1, ..., param4) .
>
> How can I call the constructor/destructor of X from the
> constructor/destructor of Y, before I the rest of the code of
> constructor/destructor of Y.


You don't 'call' a constructor explicitely. Constructors are called
automatically by compiler as a side effect of object creation.

You can instruct the compiler which constructor to use from the
base class, by specifiying it in the initialiser list.

class X
{
public:
X( int param1, int param2 ) {}
};

class Y : public X
{
public:
Y( int param1, int param2, int param3 ) : X( param2, param3 ) {}
};



>
> Can I use the 'this' keyword. It gets compiled for the destructor but doen't
> for the constructor. ( ((X*) this)->X( ... ) )
>
> When calling the constructor of X from the constructor of Y, with X(...) it
> gets compiled, but seems not to work, because I get segmentation errors at
> runtime.
>


The above is all nonsense. A constructor is a function with no name, thus
the programmer is not able to call it directly. The compiler will insert
calls to a constructor whenever an object gets created.

But all of this, including initialiser lists, should be handled in your
text book. What has it to say about it?

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Fabian Müller
Guest
Posts: n/a
 
      07-06-2004
Hi,

thanks I get the same result in the meantime by the web.

But one thing is missing for me:

When an object is created, in what order the constructors/destructors of the
base classes are called.
Lets say X is derived from Y, and Y is derived from Z.
At Object creation is first called the constructor of Z and at last the
constructor of X, which would make sense to me ?

Are the destructors of the base classes also be called automatically, as are
the constructors ?

I use the textbook "MSDN Library", which says not much to this, I think.

Many thanks again.

Fabian Müller

"Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Fabian Müller" wrote:
> >
> > Hi all,
> >
> > my question is as follows:
> >
> > If have a class X and a class Y derived from X.
> >
> > Constructor of X is X(param1, param2) .
> >
> > Constructor of Y is Y(param1, ..., param4) .
> >
> > How can I call the constructor/destructor of X from the
> > constructor/destructor of Y, before I the rest of the code of
> > constructor/destructor of Y.

>
> You don't 'call' a constructor explicitely. Constructors are called
> automatically by compiler as a side effect of object creation.
>
> You can instruct the compiler which constructor to use from the
> base class, by specifiying it in the initialiser list.
>
> class X
> {
> public:
> X( int param1, int param2 ) {}
> };
>
> class Y : public X
> {
> public:
> Y( int param1, int param2, int param3 ) : X( param2, param3 ) {}
> };
>
>
>
> >
> > Can I use the 'this' keyword. It gets compiled for the destructor but

doen't
> > for the constructor. ( ((X*) this)->X( ... ) )
> >
> > When calling the constructor of X from the constructor of Y, with X(...)

it
> > gets compiled, but seems not to work, because I get segmentation errors

at
> > runtime.
> >

>
> The above is all nonsense. A constructor is a function with no name, thus
> the programmer is not able to call it directly. The compiler will insert
> calls to a constructor whenever an object gets created.
>
> But all of this, including initialiser lists, should be handled in your
> text book. What has it to say about it?
>
> --
> Karl Heinz Buchegger
> (E-Mail Removed)



 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-06-2004
>
> Can I use the 'this' keyword. It gets compiled for the destructor but

doen't
> for the constructor. ( ((X*) this)->X( ... ) )
>


Don't do that, the destructor gets called *automatically*. If you call it
yourself then its going to get called twice.

john


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-06-2004
Fabian Müller wrote:
> But one thing is missing for me:
>
> When an object is created, in what order the constructors/destructors of the
> base classes are called.


Constructors -- in the declaration order. Destructors -- in reverse.

> Lets say X is derived from Y, and Y is derived from Z.
> At Object creation is first called the constructor of Z and at last the
> constructor of X, which would make sense to me ?
>
> Are the destructors of the base classes also be called automatically, as are
> the constructors ?


Yes.

V
 
Reply With Quote
 
Fabian Müller
Guest
Posts: n/a
 
      07-06-2004
Thanks, it now make sense to me.

Fabian

"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> >
> > Can I use the 'this' keyword. It gets compiled for the destructor but

> doen't
> > for the constructor. ( ((X*) this)->X( ... ) )
> >

>
> Don't do that, the destructor gets called *automatically*. If you call it
> yourself then its going to get called twice.
>
> john
>
>



 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-06-2004
Fabian Müller wrote:

> Hi,
>
> thanks I get the same result in the meantime by the web.
>
> But one thing is missing for me:
>
> When an object is created, in what order the constructors/destructors
> of the base classes are called.
> Lets say X is derived from Y, and Y is derived from Z.
> At Object creation is first called the constructor of Z and at last
> the constructor of X, which would make sense to me ?


Yes, it makes sense, and yes, that's the order they are called in.

> Are the destructors of the base classes also be called automatically,
> as are the constructors ?


Yes, but of course in the opposite order. Since each class only has one
single destructor, you cannot select one like you can with the
constructor. But whenever an object of a derived class is destroyed,
the destructors of all its base classes are called, too.


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-06-2004
* Karl Heinz Buchegger:
> "Fabian Müller" wrote:
> >
> > Hi all,
> >
> > my question is as follows:
> >
> > If have a class X and a class Y derived from X.
> >
> > Constructor of X is X(param1, param2) .
> >
> > Constructor of Y is Y(param1, ..., param4) .
> >
> > How can I call the constructor/destructor of X from the
> > constructor/destructor of Y, before I the rest of the code of
> > constructor/destructor of Y.

>
> You don't 'call' a constructor explicitely. Constructors are called
> automatically by compiler as a side effect of object creation.


It's a bit simpler to say that object creation is an unavoidable side
effect of a constructor call. Note that there are three concepts
involved here: allocation, object creation (from that memory), and how
to call a constructor. The discussion below (see below), as well as
all the places the HS mention constructor calls, would be meaningless
if one could not talk about constructor calls.

Of course one call a constructor explicitly; your own example below
does exactly that.

The language even allows you to call a constructor on specified
pre-existing storage, to construct an object from that storage.

On the other hand, constructor calls are special.

Just like in a call of a virtual function there are things going on
"under the hood" before your explicit C++ source level call ends
up as a machine code call-instruction. In particular, a constructor
call will generally result in other constructors also being called.
Namely constructors for members, including base class constructors.


> You can instruct the compiler which constructor to use from the
> base class, by specifiying it in the initialiser list.
>
> class X
> {
> public:
> X( int param1, int param2 ) {}
> };
>
> class Y : public X
> {
> public:
> Y( int param1, int param2, int param3 ) : X( param2, param3 ) {}
> };


Note that in the base class constructor call the actual arguments
are supplied, and evaluation of the arguments can involve function
calls etc.

This means that side-effects from arguments to constructor calls
will occur in the order Y then X

Execution of constructor bodies is in the opposite order: X, then Y.



> The above is all nonsense. A constructor is a function with no name, thus
> the programmer is not able to call it directly.


That is pure nonsense; see your own example.


> The compiler will insert
> calls to a constructor whenever an object gets created.


That is totally incorrect and misleading.

First, the compiler does not insert calls to constructors on its
own volition.

Second, objects of elementary types and POD types and arrays thereof
do not have constructors called at all.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-06-2004
* Fabian Müller:
> Hi all,
>
> my question is as follows:
>
> If have a class X and a class Y derived from X.
>
> Constructor of X is X(param1, param2) .
>
> Constructor of Y is Y(param1, ..., param4) .
>
> How can I call the constructor/destructor of X from the
> constructor/destructor of Y, before I the rest of the code of
> constructor/destructor of Y.


See example in Karl Heinz Buchegger's reply (ignore the text, though).



> Can I use the 'this' keyword. It gets compiled for the destructor but doen't
> for the constructor. ( ((X*) this)->X( ... ) )


No, that is not legal syntax.

To call a constructor on pre-existing storage you can technically use
placement new, like 'new(this) X( ... )'.

But as John Harrison replied, DON'T DO THAT, because the constructor
is already called -- you have an _implicit_ constructor call by not
specifying the call, and you can specify an _explicit_ call by using
the initializer list syntax as shown in Karl Heinz Buchegger's reply.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
SaltPeter
Guest
Posts: n/a
 
      07-06-2004

"Fabian Müller" <(E-Mail Removed)> wrote in message
news:cced7j$2nc$(E-Mail Removed)-ilmenau.de...
> Hi all,
>
> my question is as follows:
>
> If have a class X and a class Y derived from X.
>
> Constructor of X is X(param1, param2) .
>
> Constructor of Y is Y(param1, ..., param4) .
>
> How can I call the constructor/destructor of X from the
> constructor/destructor of Y, before I the rest of the code of
> constructor/destructor of Y.


Constructors aren't "called", they are invoked. The same goes for
destructors (exception: new/delete where you take over the responsability of
creation-destruction). Which constructor is invoked may be specified in a
derived class's init list. Consider:

#include <iostream>

class B
{
int m_x;
public:
B() : m_x(0) { std::cout << "default B cstor invoked\n"; }
B(int x) : m_x(x) { std::cout << "alternate B cstor invoked\n"; }
~B() { std::cout << "B d~stor invoked\n\n"; }
void display() { std::cout << "m_x = " << m_x << std::endl; }
};

class D : public B
{
int m_y;
public:
D() : m_y(0), B() { std::cout << "default D cstor invoked\n"; }
D(int y, int x) : m_y(y), B(x) { std::cout << "alternate D cstor
invoked\n"; }
~D() { std::cout << "D d~stor invoked\n"; }
void display()
{
std::cout << "m_y = " << m_y << std::endl;
B::display();
}
};

int main()
{
{ // a scope to isolate the construction / destruction phase
D d; // cstor invoked by compiler
d.display();
} // d~stor invoked by compiler

{ // isolation
D dd(1, 2); // alternate cstor invoked by compiler
dd.display();
} // d~stor invoked by compiler

D* p_D = new D(10, 20); // alternate cstor invoked by you
p_D->display();
delete p_D; // destructor invoked by you.

return 0;
}

>
> Can I use the 'this' keyword. It gets compiled for the destructor but

doen't
> for the constructor. ( ((X*) this)->X( ... ) )
>
> When calling the constructor of X from the constructor of Y, with X(...)

it
> gets compiled, but seems not to work, because I get segmentation errors at
> runtime.
>
> Many thanks to any advice.
>
> Fabian Müller
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Base class method Need base class value Karan Rajput Ruby 2 12-22-2010 04:47 PM
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
derived class incresing the sopce of base class methods and explicit call to constructor Taran C++ 6 04-20-2006 06:26 PM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM
Overload by deriv class param; call w base class param ectoplasm C++ 12 07-28-2005 08:20 AM



Advertisments