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

constructor inside constrcutor and this object

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

Apr 13 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

gopal wrote:
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

Apr 13 '06 #2

P: n/a

gopal wrote:
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!

Apr 13 '06 #3

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

Apr 13 '06 #4

P: n/a
Nothing of that sort, infact i like the FAQ very much!!!!

Apr 13 '06 #5

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

Apr 13 '06 #6

P: n/a
On 13 Apr 2006 00:54:46 -0700, "gopal" <go*********@gmail.com> wrote:
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 */}

--
Bob Hairgrove
No**********@Home.com
Apr 13 '06 #7

P: n/a
gopal <go*********@gmail.com> wrote:
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?

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

(reply address in rot13, unscramble first)
Apr 13 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.