constructor inside constrcutor and this object

G

gopal

Can one constructor of a class call another constructor of the same
class to initialize the this object?

I read in the FAQ , to the above question the following ans was given

Nope.

Let's work an example. Suppose you want your constructor Foo::Foo(char)
to call another constructor of the same class, say Foo::Foo(char,int),
in order that Foo::Foo(char,int) would help initialize the this object.
Unfortunately there's no way to do this in C++.

Some people do it anyway. Unfortunately it doesn't do what they want.
For example, the line Foo(x, 0); does not call Foo::Foo(char,int) on
the this object. Instead it calls Foo::Foo(char,int) to initialize a
temporary, local object (not this), then it immediately destructs that
temporary when control flows over the ;.


"this" object is not created

But i have the follwiing code


Foo::Foo(char *p)
{
auto int a,b;
Foo(20,30);
}

Foo::Foo(int a, int c):a(a),b(c)
{
}

I tried debugging and after i came out of the

Foo::Foo(int a, int c):a(a),b(c)
{ }

part i could see the "this" object has some valid address in watch
window

Is this correct? please can some one help me on this?

Regards
Gopal
 
M

Michiel.Salters

gopal said:
Can one constructor of a class call another constructor of the same
class to initialize the this object?

Not really. The second constructor will have a different this.
Some people do it anyway. Unfortunately it doesn't do what they want.
For example, the line Foo(x, 0); does not call Foo::Foo(char,int) on
the this object. Instead it calls Foo::Foo(char,int) to initialize a
temporary, local object (not this), then it immediately destructs that
temporary when control flows over the ;.
But i have the follwiing code

Foo::Foo(char *p)
{
auto int a,b;
Foo(20,30);
}

Foo::Foo(int a, int c):a(a),b(c)
{
}

I tried debugging and after i came out of the

Foo::Foo(int a, int c):a(a),b(c)
{ }

part i could see the "this" object has some valid address in watch
window

Is this correct? please can some one help me on this?

Well, what you see is exactly what the FAQ told you. The Foo(20,30) is
a temporary, with its own *this. Your debugger will show you the
appropriate *this, but you have to understand what's happening. Watch
closely, and you'll see (this) itself will take on two different
values.
Both pointer will be OK (they're both valid Foo* pointers) but only the
second points to a Foo for which Foo::a == 20.

HTH,
Michiel Salters
 
P

Pete C

gopal said:
Can one constructor of a class call another constructor of the same
class to initialize the this object?

I read in the FAQ , to the above question the following ans was given

Nope.

<snip>

Is this correct? please can some one help me on this?

Regards
Gopal

The FAQ is correct, of course, and it suggests a good workaround
(putting common code into a private method that is called by each
constructor).
The FAQ exists to anwser common questions about c++. If there is
something you don't understand about it, then by all means ask... but
don't ask just because you don't like what it says!
 
G

gopal

That means we will have more than one this object and no temporary
object

OR

is the temporary object is also called as this object, becuase
according to the FAQ:

Instead it calls Foo::Foo(char,int) to initialize a
temporary, local object (not this) -----> what is the exact meaning of
this?
 
P

Pete C

gopal said:
Instead it calls Foo::Foo(char,int) to initialize a
temporary, local object (not this) -----> what is the exact meaning of
this?
Foo::Foo(char *p)
{
auto int a,b;
Foo(20,30);
}

Well, the line "Foo(20,30);" creates a temporary Foo object,
initialises it with (20,30) and then destroys it before moving on to
the next line. That's how c++ parses a statement like that. I assume
that's not what you intended (it works very differently from Java, if
that's where you're coming from). Like the FAQ says, you can't call a
constructor explicitly in this way.
 
B

Bob Hairgrove

Can one constructor of a class call another constructor of the same
class to initialize the this object?

I read in the FAQ , to the above question the following ans was given

Nope.

Let's work an example. Suppose you want your constructor Foo::Foo(char)
to call another constructor of the same class, say Foo::Foo(char,int),
in order that Foo::Foo(char,int) would help initialize the this object.
Unfortunately there's no way to do this in C++.

Some people do it anyway. Unfortunately it doesn't do what they want.
For example, the line Foo(x, 0); does not call Foo::Foo(char,int) on
the this object. Instead it calls Foo::Foo(char,int) to initialize a
temporary, local object (not this), then it immediately destructs that
temporary when control flows over the ;.


"this" object is not created

But i have the follwiing code


Foo::Foo(char *p)
{
auto int a,b;
Foo(20,30);
}

Foo::Foo(int a, int c):a(a),b(c)
{
}

I tried debugging and after i came out of the

Foo::Foo(int a, int c):a(a),b(c)
{ }

part i could see the "this" object has some valid address in watch
window

Is this correct? please can some one help me on this?

According to the C++ standard, an object is only a valid object after
its constructor has finished running. However, all the members of the
object will be constructed at the end of the initialisation list (if
any) and before the constructor body begins. IOW, although you can use
the "this" pointer for some things, e.g. passing it to a parent object
which stores a vector of child object pointers, you should not
dereference "this" (i.e. "*this") until the constructor has finished.

So it really depends on what you are trying to do. If the constructor
taking a char* "knows" that its int members need to be initialised
with the values 20 and 30, then just do it, i.e.:

Foo::Foo(char *p) : a(20), b(30)
{ /* finish construction using p */}
 
J

Jakob Bieling

gopal said:
That means we will have more than one this object and no temporary
object

is the temporary object is also called as this object, becuase
according to the FAQ:

Instead it calls Foo::Foo(char,int) to initialize a
temporary, local object (not this) -----> what is the exact meaning
of this?


You are misunderstanding 'this'. There is no such thing as one 'this
object'. It is just a way to get a pointer to the object inside a member
function. Consider: if *I* say 'my hand' and *you* say 'my hand', we
both will most likely have *different* hands, but we both call it 'my
hand'. The same applies to 'this'.

So the object A you are currently constructing has *its* this
pointer pointing to A and the temporary object B has its this pointer
pointing to B.

hth
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top