Call constructor/destructor of base class

F

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.

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
 
K

Karl Heinz Buchegger

Fabian Müller said:
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?
 
F

Fabian Müller

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
 
J

John Harrison

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
 
V

Victor Bazarov

Fabian said:
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
 
F

Fabian Müller

Thanks, it now make sense to me.

Fabian

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

john
 
R

Rolf Magnus

Fabian said:
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.
 
A

Alf P. Steinbach

* Karl Heinz Buchegger:
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

Alf P. Steinbach

* 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.
 
S

SaltPeter

Fabian Müller said:
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;
}
 
J

Jerry Coffin

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

It's not _always_ a mistake to invoke the dtor yourself. It's true
that under normal circumstances, the ctor and dtor are invoked
automatically when the object is created and destroyed, respectively.

The placement new operator, however, allows you to separate memory
allocation from object creation. When you use it, you also separate
destroying the object from releasing the memory it occupies.

At least AFAIK, this is the _only_ circumstance under which it's
reasonable to directly invoke a dtor though.
 
K

Karl Heinz Buchegger

Alf P. Steinbach said:
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.

<quote>

12.1 Constructors
1 Constructors do not have names. A special declrator syntax using ....

2 A constructor is used to initialize objects. Because constructors don't
have names, they are never found during name lookup; ...

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

How can one call a function that has no name na dis not found during
name lookup?

I find it simpler to think about the invokation of a constructor as a side
effect of object creation.
The language even allows you to call a constructor on specified
pre-existing storage, to construct an object from that storage.

Exactly: The intent is to create an object in an area of memory.
As a side effect of that object creation, the constructor is called.

[snip]
That is pure nonsense; see your own example.

Who I am to arguehow C++ works. See the standard.
That is totally incorrect and misleading.

In which way?
First, the compiler does not insert calls to constructors on its
own volition.

The compiler inserts calls to constructors whenever the programmer
request to create an object. If you like it or not, but this is
how it works.
 
A

Alf P. Steinbach

* Karl Heinz Buchegger:
<quote>

12.1 Constructors
1 Constructors do not have names. A special declrator syntax using ....

2 A constructor is used to initialize objects. Because constructors don't
have names, they are never found during name lookup; ...

</quote>

That is presumably a correct quote.

How can one call a function that has no name na dis not found during
name lookup?

See your own example.


I find it simpler to think about the invokation of a constructor as a side
effect of object creation.

Then you need to differentiate between different kinds objects, for one
thing (which you failed to do in your previous posting, and additionally
failed to do in this posting (see below)).

For another, it leads you into a quagmire of silly termininological
contortions.

I think that's why you don't understand this, as evidenced by that
repeated failure mentioned above.


Exactly: The intent is to create an object in an area of memory.
As a side effect of that object creation, the constructor is called.

Normally my response to such would be 'idiot', but you're far from
being an idiot. I think you're lying. About what you understand.

Who I am to arguehow C++ works. See the standard.

The standard indeed states that a constructor does not have a name.

It does of course not support your unsound inference from that.


In which way?

<quote>
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.
The compiler inserts calls to constructors whenever the programmer
request to create an object. If you like it or not, but this is
how it works.

The code snippet

int x;

creates an object. Give chapter and verse in the standard for the alleged
constructor call, please.
 
K

Karl Heinz Buchegger

Alf P. Steinbach said:
That is presumably a correct quote.
Fine.


See your own example.

I didn't call a constructor. I created an object.
Then you need to differentiate between different kinds objects, for one
thing (which you failed to do in your previous posting, and additionally
failed to do in this posting (see below)).

Yes, I need to do that. I didn't in the OP's case, because it was clear
(at least for me) that in his situation this information is not needed
to answer his specific question.
For another, it leads you into a quagmire of silly termininological
contortions.

I think that's why you don't understand this, as evidenced by that
repeated failure mentioned above.

Oh, I understand it perfectly.
Normally my response to such would be 'idiot',

Why idiot? You said it for yourself. ... to construct an object from
that storage.

Construct an object, not ... call a constructor ...
<quote>
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.
</quote>

OK. You got me here. For elementry types, POD types and arrays
thereof, no constructor is called, because this things don't
have a constructor. Thanks for pointing this out.
Objects of this types are special.
The code snippet

int x;

creates an object. Give chapter and verse in the standard for the alleged
constructor call, please.

I can't. An int object is special.

But even in this light: There is no syntax for a programmer to explicitely
call a constructor. Constructor calls are inserted by the compiler as
side effects of object creation (if the object is not of type elementary,
POD or an array ...)

This is exactly the OP's problem in

class X
{
public:
void foo();
};

class Y : public X
{
public:
Y() {
X(); // 1
foo(); // 2
}
};

Even if // 1 looks identical to // 2 it does not do the same thing.
While // 2 calls foo() for this object, // 1 does not call the
constructor for this object, but instead creates a temporary and
as a side effect of that creation calls the constructor. Not
for this object but for the temporary.

If you think otherwise then: chapter and verse of where it is stated that
a constructor can be called and what the syntax for doing this is. I only
know the section where it is stated, that a constructor is not found during
name lookup and name lookup is an important part in the compilers work to
determine which function to call.
 
A

Alf P. Steinbach

* Karl Heinz Buchegger:
Yes, I need to do that. I didn't in the OP's case, because it was clear
(at least for me) that in his situation this information is not needed
to answer his specific question.

Sometimes that is a Good Idea.

When the holding back of detailed information creates a false
impression then it is a Bad Idea (TM).

I can't. An int object is special.

But even in this light: There is no syntax for a programmer to explicitely
call a constructor.

See your own example.

And if you leave out the explicit constructor call you have there, you
will get an implicit constructor call.

The difference between explicit and implicit is: implicit, you do not
include a call in the program text; explicit, you do include a call.

Constructor calls are inserted by the compiler as
side effects of object creation (if the object is not of type elementary,
POD or an array ...)
Nope.


This is exactly the OP's problem in

class X
{
public:
void foo();
};

class Y : public X
{
public:
Y() {
X(); // 1
foo(); // 2
}
};

Even if // 1 looks identical to // 2 it does not do the same thing.

That is correct.


If you think otherwise

I don't.

then: chapter and verse of where it is stated that
a constructor can be called

There are numerous such places in the standard. But to avoid any
misunderstanding, let me first state categorically that this does _not_
mean I think "otherwise" re your example above. Let me repeat that: it
does not mean that I, or the standard, thinks "otherwise" re your example
above. Wrt. your example above you have correct understanding. Now (if
that's OK?) see e.g. §12.1/5, "a default constructor for a class X is a
constructor of class X that can be called without without an argument".

and what the syntax for doing this is.

See your own examples in earlier posting and in this one.
 
K

Karl Heinz Buchegger

Alf P. Steinbach said:
There are numerous such places in the standard. But to avoid any
misunderstanding, let me first state categorically that this does _not_
mean I think "otherwise" re your example above. Let me repeat that: it
does not mean that I, or the standard, thinks "otherwise" re your example
above. Wrt. your example above you have correct understanding. Now (if
that's OK?) see e.g. §12.1/5, "a default constructor for a class X is a
constructor of class X that can be called without without an argument".

That doesn't answer my question, since it doesn't say how I (the programmer)
have to code a call to a constructor. It merly says: It can be called, eg.
during the process of creating an object, a constructor may be called.

What is the syntax for calling a constructor and where is this syntax
mentioned in the standard?
See your own examples in earlier posting and in this one.

Again: I did not 'call' a constructor just like I call any other
ordinary function. I created an object and during the creation of
that object the compiler arranged the calling of the constructor.
So, the reason for calling that constructor was not that I programmed
a function call statement, but I instructed the compiler to create an object
and as part of that object creation a constructor is called.
 
A

Alf P. Steinbach

* Karl Heinz Buchegger:
That doesn't answer my question, since it doesn't say how I (the programmer)
have to code a call to a constructor. It merly says: It can be called, eg.
during the process of creating an object, a constructor may be called.

You ignore the part of that very short quote which was the reason I
carefully selected it for you.

It says the default constructor can be called _without an argument_, and
that is necessarily a source code explicit call: it does not make sense in
generated code, which must supply any arguments in the argument list.

What is the syntax for calling a constructor

See your own examples, and RTFM.
 
K

Karl Heinz Buchegger

Alf P. Steinbach said:
* Karl Heinz Buchegger:

You ignore the part of that very short quote which was the reason I
carefully selected it for you.

It says the default constructor can be called _without an argument_, and
that is necessarily a source code explicit call:

How come?
From _without an argument_ does not follow that a programmer can explicitely
call a constructor.
As said: during the construction of an object the compiler has to call the
constructor. If the object construction is done without arguments the compiler
can use the default constructor. That's all the above is saying.
From this it does not follow that one can explicitely call a constructor.
it does not make sense in
generated code, which must supply any arguments in the argument list.



See your own examples, and RTFM.

I did. And the FM seems to support my view.

You have still not shown how an explicit call to a constructor would
look like in a program and shown in which way the standard states that
this is an explicit constructor call (That is: a function call which does
not originate in the creation of an object)
 
A

Alf P. Steinbach

* Karl Heinz Buchegger:
How come?

You happily snipped the explanation, here it is again:

<quote>
[the phrasing "without an argument"] does not make sense in
generated code, which must supply any arguments in the argument
list.
</quote>

It seems you have a habit of not seeing things you don't like.

Note: the word "explicit" means the opposite of "implicit".

If the object construction is done without arguments the compiler
can use the default constructor. That's all the above is saying.

No, it says, quote: "can be called without an argument".

You have still not shown how an explicit call to a constructor would
look like in a program

See your own examples, and RTFM _not ignoring_ what you don't like.
 
K

Karl Heinz Buchegger

Alf P. Steinbach said:
* Karl Heinz Buchegger:
How come?

You happily snipped the explanation, here it is again:

<quote>
[the phrasing "without an argument"] does not make sense in
generated code, which must supply any arguments in the argument
list.
</quote>

It seems you have a habit of not seeing things you don't like.

Note: the word "explicit" means the opposite of "implicit".
If the object construction is done without arguments the compiler
can use the default constructor. That's all the above is saying.

No, it says, quote: "can be called without an argument".

But it doesn't say by whom.
And that is the whole point!!!!!!!!

I do not claim, that a constructor is not callable.
I only claim that for the programmer there is no way to instruct
the compiler to do a call to the constructor, that is, not in the
context of creating an object.

That's all. Is this so hard to understand?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top