How to call a constructor from within another constructor

Discussion in 'C++' started by Andy, Sep 24, 2006.

  1. Andy

    Andy Guest

    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
    Andy, Sep 24, 2006
    #1
    1. Advertising

  2. Thomas Tutone, Sep 24, 2006
    #2
    1. Advertising

  3. Andy

    Andy Guest

    Thomas Tutone wrote:
    > This is an FAQ. See:


    Hi Tom,

    Thanks for the fast answer. It has helped me solving my problem.

    Regards,
    Andreas
    Andy, Sep 24, 2006
    #3
  4. Andy

    loufoque Guest

    Andy wrote :
    > is it in C++ possible to call a constructor from another constructor?


    No.
    I think they want to fix it for C++0x though.
    loufoque, Sep 24, 2006
    #4
  5. 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 */
    }
    };

    --

    Frederick Gotham
    Frederick Gotham, Sep 24, 2006
    #5
  6. Frederick Gotham wrote:

    > 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 */
    > }
    > };


    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
    Thomas Tutone, Sep 24, 2006
    #6
  7. Andy

    Greg Guest

    Frederick Gotham wrote:
    > 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 */
    > }
    > };


    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
    Greg, Sep 24, 2006
    #7
  8. 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.)

    --

    Frederick Gotham
    Frederick Gotham, Sep 24, 2006
    #8
  9. * Thomas Tutone:
    > Frederick Gotham wrote:
    >>
    >> 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.


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



    > This is a hack, not an idiom, and is not good advice to give to an
    > inexperienced C++ user.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Sep 24, 2006
    #9
  10. Andy

    Pete Becker Guest

    Frederick Gotham wrote:
    >
    > 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.
    Pete Becker, Sep 24, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brett Irving
    Replies:
    3
    Views:
    3,333
    John Harrison
    Jun 29, 2003
  2. Matt Graham
    Replies:
    3
    Views:
    510
    Matt Graham
    Feb 7, 2004
  3. Asfand Yar Qazi
    Replies:
    6
    Views:
    15,624
    jeffc
    May 17, 2004
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,217
  5. Warren Tang
    Replies:
    9
    Views:
    668
    James Kanze
    Apr 15, 2008
Loading...

Share This Page