Click to See Complete Forum and Search --> : A constructor calls another constructor of the same class?
dullboy
January 26th, 2005, 10:31 AM
class Foo {
public:
Foo(char x);
Foo(char x, int y);
...
};
Foo::Foo(char x)
{
...
Foo(x, 0); // this line does NOT help initialize the this object!!
...
}
My questions is why Foo(x,0) doesn't help initialize the this object? Thanks for your suggestions.
Paul McKenzie
January 26th, 2005, 10:34 AM
Foo::Foo(char x)
{
...
Foo(x, 0); // this line does NOT help initialize the this object!!
...
}
My questions is why Foo(x,0) doesn't help initialize the this object? Thanks for your suggestions.Because Foo(x,0) is a brand-new temporary object. It has no influence on the current object.
Regards,
Paul McKenzie
cilu
January 26th, 2005, 10:40 AM
class foo
{
char m_x;
public:
foo(char x = 0); // 0 is the default value for x
void print();
}
foo::foo(char x) : m_x(x) // m_x = x
{
}
void foo:print()
{
cout << m_x << endl;
}
In this case you can create an object
foo f1;
f1.print(); // prints 0
foo f2(2);
f2.print(); // prints 2
And please use the C++ (Non VisualC++ Issues) Forum (http://www.codeguru.com/forum/forumdisplay.php?f=9) for non visual questions.
dullboy
January 26th, 2005, 10:50 AM
Thanks for your reply. Perhaps my question should be that how a constructor of a class constructs "this" object?
Because Foo(x,0) is a brand-new temporary object. It has no influence on the current object.
Regards,
Paul McKenzie
cilu
January 26th, 2005, 10:59 AM
this is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer. An object's this pointer is not part of the object itself; it is not reflected in the result of a sizeof statement on the object. Instead, when a nonstatic member function is called for an object, the address of the object is passed by the compiler as a hidden argument to the function.
dullboy
January 26th, 2005, 10:59 AM
Thanks for your reply. But I can't see your answer is related to my question. Please advise. I think often people here are more helpful. That is why I prefer posting my questions here.
class foo
{
char m_x;
public:
foo(char x = 0); // 0 is the default value for x
void print();
}
foo::foo(char x) : m_x(x) // m_x = x
{
}
void foo:print()
{
cout << m_x << endl;
}
In this case you can create an object
foo f1;
f1.print(); // prints 0
foo f2(2);
f2.print(); // prints 2
And please use the C++ (Non VisualC++ Issues) Forum (http://www.codeguru.com/forum/forumdisplay.php?f=9) for non visual questions.
dullboy
January 26th, 2005, 11:04 AM
Actually, what I am concerned about at this point is how "this" is initialized in the constructor.
this is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer. An object's this pointer is not part of the object itself; it is not reflected in the result of a sizeof statement on the object. Instead, when a nonstatic member function is called for an object, the address of the object is passed by the compiler as a hidden argument to the function.
Paul McKenzie
January 26th, 2005, 11:25 AM
Actually, what I am concerned about at this point is how "this" is initialized in the constructor.I don't understand your question, or your concern for that matter. In the body of the constructor, this is valid.
I don't know what else to tell you. The compiler does what it does. Just follow the rules -- you're constructing a new object in C++ when you invoke the constructor, regardless of where you may be constructing the new object. The fact that you are constructing a Foo() object in the Foo() constructor is just a matter of consequence -- nothing special or extraordinary is occuring.
Also, this question should be posted in the non-Visual C++ section. There is just as much help there (maybe more so), when it comes to general C++ questions.
Regards,
Paul McKenzie
dullboy
January 26th, 2005, 11:37 AM
Perhaps my question sounds stupid --- How can I EXPLICITLY initialize "this" in a constructor?
I don't understand your question, or your concern for that matter. In the body of the constructor, this is valid.
I don't know what else to tell you. The compiler does what it does. Just follow the rules -- you're constructing a new object in C++ when you invoke the constructor, regardless of where you may be constructing the new object. The fact that you are constructing a Foo() object in the Foo() constructor is just a matter of consequence -- nothing special or extraordinary is occuring.
Also, this question should be posted in the non-Visual C++ section. There is just as much help there (maybe more so), when it comes to general C++ questions.
Regards,
Paul McKenzie
Paul McKenzie
January 26th, 2005, 12:16 PM
Perhaps my question sounds stupid --- How can I EXPLICITLY initialize "this" in a constructor?From what it looks like you're trying to do, why not just have another function that does what you want, and call it from the constructor? Maybe call it Init() or something like that.
class Foo
{
public:
Foo(char x)
{
Init(x, 0);
}
Foo(char x, int y)
{
Init(x, y);
}
private:
void Init(char x, int y)
{
// whatever
}
};
Regards,
Paul McKenzie
Marc G
January 26th, 2005, 01:16 PM
Follow Paul McKenzies' suggestion.
In C++ you cannot call another constructor for the same object from within that object.
Andreas Masur
January 26th, 2005, 02:57 PM
[ Moved thread ]
JohnCz
January 26th, 2005, 04:54 PM
What about this approach?
class Foo
{
public:
Foo(char x, char y = 0);
virtual ~Foo();
char m_x;
char m_y;
};
Foo::Foo(char x, char y/* = 0*/)
: m_x(x)
, m_y(y)
{
}
Foo::~Foo()
{
}
cilu
January 27th, 2005, 02:57 AM
Perhaps my question sounds stupid --- How can I EXPLICITLY initialize "this" in a constructor?
The way I see it, is that you don't know what "this" is. I have to repeat myself. this is not another member object of the class. this is a const pointer, which is the address of the object whose method is called. When you call the constructor (as well as any other method) the address of the object is passed as a hidden parameter. And you can use this pointer to refer to the object itself. So you cannot speak about a special initialization of this pointer, rather than the object. You must see this as an alter ego of the object.
Graham
January 27th, 2005, 03:49 AM
Perhaps my question sounds stupid --- How can I EXPLICITLY initialize "this" in a constructor? Possibly the reason for the confusion is in that statement. Do you mean that you want to set the value of this? I.e. that you want to set the address of the object? Or that you want to explicitly initialise *this - i.e. the member variables of the object pointed to?
For the former, the only way to do that is to use placement new - i.e. allocate some storage and construct the object there.
For the latter: well, that's exactly what the constructor is doing, and this is where the confusion may be creeping in - you seem to be asking how to do something that you are already doing. The suggestions given so far should help.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.