How to call a constructor from within another constructor

A

Andy

Hi,

is it in C++ possible to call a constructor from another constructor?
In Java you can do the following:

public class MyClass
{
public MyClass()
{
this(10);
}

public MyClass(int someNumber)
{
...
}

...
}

How can I do something equal in C++?

Regards,
Andy
 
F

Frederick Gotham

Andy posted:
public class MyClass
{
public MyClass()
{
this(10);
}

public MyClass(int someNumber)
{
...
}

...
}


I'd don't see any problem with the following:

#include <new>

struct MyClass {
public:

MyClass(double a, int b)
{
::new(this) MyClass(a * 3 / b);
}

MyClass(unsigned const i)
{
/* Code */
}
};
 
T

Thomas Tutone

Frederick said:
Andy posted:



I'd don't see any problem with the following:

#include <new>

struct MyClass {
public:

MyClass(double a, int b)
{
::new(this) MyClass(a * 3 / b);
}

MyClass(unsigned const i)
{
/* Code */
}
};

You may not see any problem with this, but most C++ authorities do.
This is a hack, not an idiom, and is not good advice to give to an
inexperienced C++ user.

Best regards,

Tom
 
G

Greg

Frederick said:
Andy posted:



I'd don't see any problem with the following:

#include <new>

struct MyClass {
public:

MyClass(double a, int b)
{
::new(this) MyClass(a * 3 / b);
}

MyClass(unsigned const i)
{
/* Code */
}
};

One problem is that MyClass's data members are constructed and then
constructed again without ever having been destroyed in the interim. So
any code that relies on the MyClass's data members being properly
destroyed, now has undefined behavior.

A MyClass static factory method, or a non-virtual derived class of
MyClass that declares only constructors would be two safer workarounds
- at least until compilers add support for C++0x's delegating
constructors.

Greg
 
F

Frederick Gotham

Greg posted:
One problem is that MyClass's data members are constructed and then
constructed again without ever having been destroyed in the interim.


Wups, I didn't catch that. (But of course, it'd be no problem if all members
had trivial destructors.)
 
A

Alf P. Steinbach

* Thomas Tutone:
You may not see any problem with this, but most C++ authorities do.

The FAQ item "Can one constructor of a class call another constructor of
the same class to initialize the this object?", currently at <url:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.3>, mentions
that "Constructors do a bunch of little magical things behind the
scenes, but that bad technique steps on those ... bits".

In addition the above hack, even if it works as literally shown above
with some specific compiler, is incompatible with inheritance and with
class-type data members. Consider a MyClass that has a std::string
member myString. myString will then be constructed twice, with no
corresponding destructor call for the first constructor call, most
likely resulting in a memory leak (no deallocation of first buffer). At
the very least there should be an explicit destructor call before the
placement new. Which goes to show that this is very easy to get wrong.

And so on -- it gets even worse when the placement new idea is used in
e.g. operator= (see <url: http://www.gotw.ca/gotw/023.htm>).
 
P

Pete Becker

Frederick said:
I'd don't see any problem with the following:

#include <new>

struct MyBase
{
MyBase() { ptr = new int; }
~MyBase() { delete ptr; }
int *ptr;
};

struct MyClass : MyBase {
MyClass(double a, int b)
{
::new(this) MyClass(a * 3 / b);
}

MyClass(unsigned const i)
{
/* Code */
}
};

Assuming that the original worked, with this change, the code has a
memory leak. So even if it's legal (I have my doubts about constructing
an object on top of the bits that are in the middle of being initialized
by another constructor), it's fragile.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,729
Latest member
ScarlettJe

Latest Threads

Top