By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,611 Members | 1,635 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,611 IT Pros & Developers. It's quick & easy.

Call constructor/destructor of base class

P: n/a
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


Jul 22 '05 #1
Share this Question
Share on Google+
23 Replies


P: n/a
"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
kb******@gascad.at
Jul 22 '05 #2

P: n/a
>
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
Jul 22 '05 #3

P: n/a
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" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
"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
kb******@gascad.at

Jul 22 '05 #4

P: n/a
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
Jul 22 '05 #5

P: n/a
Thanks, it now make sense to me.

Fabian

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2k************@uni-berlin.de...

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

Jul 22 '05 #6

P: n/a
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.
Jul 22 '05 #7

P: n/a
* 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?
Jul 22 '05 #8

P: n/a
* 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?
Jul 22 '05 #9

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2k************@uni-berlin.de>...

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.


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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #10

P: n/a

"Fabian Müller" <fa****@gmx.de> wrote in message
news:cc**********@piggy.rz.tu-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

Jul 22 '05 #11

P: n/a
"Alf P. Steinbach" wrote:


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; ...

</quote>
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]
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.


Who I am to arguehow C++ works. See the standard.
The compiler will insert
calls to a constructor whenever an object gets created.
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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #12

P: n/a
* Karl Heinz Buchegger:
"Alf P. Steinbach" wrote:


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; ...

</quote>


That is presumably a correct quote.

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?


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


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.

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.


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.
The compiler will insert
calls to a constructor whenever an object gets created.


That is totally incorrect and misleading.


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.
</quote>

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.
--
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?
Jul 22 '05 #13

P: n/a
"Alf P. Steinbach" wrote:

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


Fine.

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?


See your own example.


I didn't call a constructor. I created an object.

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)).


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.

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.


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 ...
> The compiler will insert
> calls to a constructor whenever an object gets created.

That is totally incorrect and misleading.


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.
</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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #14

P: n/a
* Karl Heinz Buchegger:
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)).


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).

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.


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.

--
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?
Jul 22 '05 #15

P: n/a
"Alf P. Steinbach" wrote:


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?
and what the syntax for doing this is.


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #16

P: n/a
* Karl Heinz Buchegger:
"Alf P. Steinbach" wrote:

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.


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.

--
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?
Jul 22 '05 #17

P: n/a
"Alf P. Steinbach" wrote:

* Karl Heinz Buchegger:
"Alf P. Steinbach" wrote:

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.


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.

What is the syntax for calling a constructor


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)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #18

P: n/a
* Karl Heinz Buchegger:
"Alf P. Steinbach" wrote:

* Karl Heinz Buchegger:
"Alf P. Steinbach" wrote:
>
> 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.
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?


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.

--
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?
Jul 22 '05 #19

P: n/a
"Alf P. Steinbach" wrote:

* Karl Heinz Buchegger:
"Alf P. Steinbach" wrote:

* Karl Heinz Buchegger:
> "Alf P. Steinbach" wrote:
> >
> > 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.

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?


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?

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.

--
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?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #20

P: n/a
* Karl Heinz Buchegger:
"Alf P. Steinbach" wrote:

* Karl Heinz Buchegger:
"Alf P. Steinbach" wrote:
>
> * Karl Heinz Buchegger:
> > "Alf P. Steinbach" wrote:
> > >
> > > 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.
>
> 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?
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.


It does; see above. Note in particular that "without any argument" can
not refer to a generated call. So it must refer to a source code call.

(In fact that's the point of that particular paragraph: if you
provide default values for all arguments of a constructor, then
it becomes a default constructor because then it "can be called
without an argument" in the source code; the paragraph is simply
a definition of what a default constructor is, based on how it can
be called in the source code.)

And that is the whole point!!!!!!!!


"Multiple explanation marks are a true sign of a diseased mind"
(Terry Pratchett) ;)

--
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?
Jul 22 '05 #21

P: n/a
"Alf P. Steinbach" wrote:


I am getting tired of your ignorance.
The standard clearly states: there is no way to explicitely call a constructor.
I leave it with that and bail out.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #22

P: n/a
* Karl Heinz Buchegger:
"Alf P. Steinbach" wrote:
I am getting tired of your ignorance.


Liar.

The standard clearly states: there is no way to explicitely call a constructor.
It does not.

It does on the other hand state numerous places that you can call them,
and I've given you one such reference.

It does that because you can.

I leave it with that and bail out.


--
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?
Jul 22 '05 #23

P: n/a

"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@news.individual.net...
* Karl Heinz Buchegger:
"Alf P. Steinbach" wrote:
I am getting tired of your ignorance.


Liar.

The standard clearly states: there is no way to explicitely call a

constructor.
It does not.

It does on the other hand state numerous places that you can call them,
and I've given you one such reference.

It does that because you can.


There is a huge difference between calling a member function and invoking a
cstor or d~stor.

Its neccessary that a coder understands the difference between constructing
an object and calling, say, one of a couple of overloaded functions, for
example. Imagine a classA object that provides a default constructor and a
ClassA(int) constructor, not to mention a copy constructor. Regardless of
which constructor is invoked, the resulting instance is identical in type.
This result can't be said of a couple of overloaded member functions. The 2
member functions represent unique behaviours and unique signatures.

The question isn't what the standard says, consider multple constructors
employed to create the exact same type of object. While a derived class can
hide or modify a base class's virtual functions, it can't replace or modify
the base class's constructor(s). Thats because the derived class can't call
the base class's constructor(s), it can only invoke them.

Jul 22 '05 #24

This discussion thread is closed

Replies have been disabled for this discussion.