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