A constructor calls another constructor of the same class?
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.
Re: A constructor calls another constructor of the same class?
Quote:
Originally Posted by dullboy
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
Re: A constructor calls another constructor of the same class?
Code:
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
Code:
foo f1;
f1.print(); // prints 0
foo f2(2);
f2.print(); // prints 2
And please use the C++ (Non VisualC++ Issues) Forum for non visual questions.
Re: A constructor calls another constructor of the same class?
Thanks for your reply. Perhaps my question should be that how a constructor of a class constructs "this" object?
Quote:
Originally Posted by Paul McKenzie
Because Foo(x,0) is a brand-new temporary object. It has no influence on the current object.
Regards,
Paul McKenzie
Re: A constructor calls another constructor of the same class?
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.
Re: A constructor calls another constructor of the same class?
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.
Quote:
Originally Posted by cilu
Code:
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
Code:
foo f1;
f1.print(); // prints 0
foo f2(2);
f2.print(); // prints 2
And please use the
C++ (Non VisualC++ Issues) Forum for non visual questions.
Re: A constructor calls another constructor of the same class?
Actually, what I am concerned about at this point is how "this" is initialized in the constructor.
Quote:
Originally Posted by cilu
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.
Re: A constructor calls another constructor of the same class?
Quote:
Originally Posted by dullboy
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
Re: A constructor calls another constructor of the same class?
Perhaps my question sounds stupid --- How can I EXPLICITLY initialize "this" in a constructor?
Quote:
Originally Posted by Paul McKenzie
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
Re: A constructor calls another constructor of the same class?
Quote:
Originally Posted by dullboy
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.
Code:
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
Re: A constructor calls another constructor of the same class?
Follow Paul McKenzies' suggestion.
In C++ you cannot call another constructor for the same object from within that object.
Re: A constructor calls another constructor of the same class?
Re: A constructor calls another constructor of the same class?
What about this approach?
Code:
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()
{
}
Re: A constructor calls another constructor of the same class?
Quote:
Originally Posted by dullboy
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.
Re: A constructor calls another constructor of the same class?
Quote:
Originally Posted by dullboy
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.