Is this allowed?
Object::Object()
{
new (this) Object(0, NULL);
}
Is this allowed?
Object::Object()
{
new (this) Object(0, NULL);
}
Using new(this) will re-construct member variables. This can result in undefined behavior, since they're not destructed first. The usual pattern is to use a helper function instead:
class Object {
private:
void init(int, char *);
public:
Object();
Object(int, char *);
};
Object::Object() {
init(0, NULL);
}
Object::Object(int x, char *y) {
init(x, y);
}
void Object::init(int x, char *y) {
/* ... */
}
I believe you want delegate constructors, like Java for example, which are not here yet. When C++0x comes you could do it like this :
Object::Object() : Object(0, NULL)
{
}
If Object
is a POD type you could initialize it in this way:
class Object
{
int x;
int y;
// ...
public:
Object() { memset( this, 0, sizeof Object ); }
};