Does C++ allow a constructor to call another constructor of the sameclass?

Discussion in 'C++' started by Warren Tang, Apr 15, 2008.

  1. Warren Tang

    Warren Tang Guest

    Hello,

    Does C++ allow a constructor to call another constructor of the same
    class? (I am focusing on the language abilities. I know the C# language
    allow this.)

    The following sample can explain this question more clearer:

    class MyClass
    {
    public:
    MyClass()
    {
    //Do something.
    }
    MyClass(int a, int b)
    {
    //I need to call the parameter-less constructor. How?
    }

    Thank you for your attention.

    Regards
    Warren
    Warren Tang, Apr 15, 2008
    #1
    1. Advertising

  2. Warren Tang

    Warren Tang Guest

    Re: Does C++ allow a constructor to call another constructor of thesame class?

    I found this:
    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.3
    which states that it's not possible.


    Warren Tang wrote:
    > Hello,
    >
    > Does C++ allow a constructor to call another constructor of the same
    > class? (I am focusing on the language abilities. I know the C# language
    > allow this.)
    >
    > The following sample can explain this question more clearer:
    >
    > class MyClass
    > {
    > public:
    > MyClass()
    > {
    > //Do something.
    > }
    > MyClass(int a, int b)
    > {
    > //I need to call the parameter-less constructor. How?
    > }
    >
    > Thank you for your attention.
    >
    > Regards
    > Warren
    Warren Tang, Apr 15, 2008
    #2
    1. Advertising

  3. Warren Tang

    Ian Collins Guest

    Re: Does C++ allow a constructor to call another constructor of thesame class?

    Warren Tang wrote:
    > Hello,
    >
    > Does C++ allow a constructor to call another constructor of the same
    > class?


    No, if you have common initialisation code, it can go in a member function.

    --
    Ian Collins.
    Ian Collins, Apr 15, 2008
    #3
  4. Warren Tang

    Warren Tang Guest

    Re: Does C++ allow a constructor to call another constructor of thesame class?

    Thank you.

    Ian Collins wrote:
    > Warren Tang wrote:
    >> Hello,
    >>
    >> Does C++ allow a constructor to call another constructor of the same
    >> class?

    >
    > No, if you have common initialisation code, it can go in a member function.
    >
    Warren Tang, Apr 15, 2008
    #4
  5. Re: Does C++ allow a constructor to call another constructor of thesame class?

    On Apr 15, 12:21 pm, Warren Tang <> wrote:
    > Hello,
    >
    > Does C++ allow a constructor to call another constructor of the same
    > class? (I am focusing on the language abilities. I know the C# language
    > allow this.)
    >
    > The following sample can explain this question more clearer:
    >
    > class MyClass
    > {
    > public:
    > MyClass()
    > {
    > //Do something.
    > }
    > MyClass(int a, int b)
    > {
    > //I need to call the parameter-less constructor. How?
    > }
    >
    > Thank you for your attention.
    >
    > Regards
    > Warren


    Yes Absolute!

    int _a, _b;

    MyClass()
    {
    _a = 10;
    _b = 20;
    }
    MyClass(int a, int b)
    {
    _a = a;
    _b = b;

    MyClass(); // this->MyClass(); // more precisely
    }

    the result will be _a = 10, _b = 20 even you create an instance by
    calling

    MyClass * _pClass = new MyClass(100, 100);

    cheers,
    Alexander Dong Back Kim, Apr 15, 2008
    #5
  6. Warren Tang

    Warren Tang Guest

    Re: Does C++ allow a constructor to call another constructor of thesame class?

    I've just read this:
    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.3

    That is a new object intuitively, so I don't like this way even the
    compiler really has special support for it.

    Anyway thanks for your comment.


    Alexander Dong Back Kim wrote:
    > On Apr 15, 12:21 pm, Warren Tang <> wrote:
    >> Hello,
    >>
    >> Does C++ allow a constructor to call another constructor of the same
    >> class? (I am focusing on the language abilities. I know the C# language
    >> allow this.)
    >>
    >> The following sample can explain this question more clearer:
    >>
    >> class MyClass
    >> {
    >> public:
    >> MyClass()
    >> {
    >> //Do something.
    >> }
    >> MyClass(int a, int b)
    >> {
    >> //I need to call the parameter-less constructor. How?
    >> }
    >>
    >> Thank you for your attention.
    >>
    >> Regards
    >> Warren

    >
    > Yes Absolute!
    >
    > int _a, _b;
    >
    > MyClass()
    > {
    > _a = 10;
    > _b = 20;
    > }
    > MyClass(int a, int b)
    > {
    > _a = a;
    > _b = b;
    >
    > MyClass(); // this->MyClass(); // more precisely
    > }
    >
    > the result will be _a = 10, _b = 20 even you create an instance by
    > calling
    >
    > MyClass * _pClass = new MyClass(100, 100);
    >
    > cheers,
    Warren Tang, Apr 15, 2008
    #6
  7. Warren Tang

    Ian Collins Guest

    Re: Does C++ allow a constructor to call another constructor of thesame class?

    Alexander Dong Back Kim wrote:
    >>
    >> Does C++ allow a constructor to call another constructor of the same
    >> class?

    >
    > Yes Absolute!
    >

    Absolutely no!

    > int _a, _b;
    >
    > MyClass()
    > {
    > _a = 10;
    > _b = 20;
    > }
    > MyClass(int a, int b)
    > {
    > _a = a;
    > _b = b;
    >
    > MyClass(); // this->MyClass(); // more precisely
    > }
    >

    Let's tidy that up a bit and define the class:

    struct MyClass
    {
    int a, b;

    MyClass()
    : a(10), b(20)
    {
    a = 10;
    b = 20;
    }

    MyClass(int a, int b)
    : a(a), b(b)
    {
    MyClass();
    }
    };

    All the statement "MyClass();" in the second constructor is create a new
    default initialised MyClass. So you can call another MyClass
    constructor, but that creates a new instance. It most definitely is not
    the same as this->MyClass();

    If we were to write

    MyClass(int a, int b)
    : a(a), b(b)
    {
    this->MyClass();
    }

    We would have a syntax error.

    > the result will be _a = 10, _b = 20 even you create an instance by
    > calling
    >
    > MyClass * _pClass = new MyClass(100, 100);
    >

    No, it will not.

    --
    Ian Collins.
    Ian Collins, Apr 15, 2008
    #7
  8. Warren Tang

    Guest

    Re: Does C++ allow a constructor to call another constructor of thesame class?

    On Apr 15, 7:21 am, Warren Tang <> wrote:
    > Hello,
    >
    > Does C++ allow a constructor to call another constructor of the same
    > class? (I am focusing on the language abilities. I know the C# language
    > allow this.)
    >
    > The following sample can explain this question more clearer:
    >
    > class MyClass
    > {
    > public:
    >         MyClass()
    >         {
    >                 //Do something.
    >         }
    >         MyClass(int a, int b)
    >         {
    >                 //I need to call the parameter-less constructor. How?
    >         }
    >
    > Thank you for your attention.
    >
    > Regards
    > Warren


    If your intention is to //Do something that is useful, you may very
    well wrap that up in a member function like Init and call it from
    either of the constructors.
    IMHO, it isn't c++ way to call the default constructor from
    parameterized constructor, the point being construction of objects.

    Thanks,
    Balaji.
    , Apr 15, 2008
    #8
  9. Re: Does C++ allow a constructor to call another constructor of thesame class?

    On Apr 15, 1:41 pm, Ian Collins <> wrote:
    > Alexander Dong Back Kim wrote:
    >
    > >> Does C++ allow a constructor to call another constructor of the same
    > >> class?

    >
    > > Yes Absolute!

    >
    > Absolutely no!
    >
    > > int _a, _b;

    >
    > > MyClass()
    > > {
    > > _a = 10;
    > > _b = 20;
    > > }
    > > MyClass(int a, int b)
    > > {
    > > _a = a;
    > > _b = b;

    >
    > > MyClass(); // this->MyClass(); // more precisely
    > > }

    >
    > Let's tidy that up a bit and define the class:
    >
    > struct MyClass
    > {
    > int a, b;
    >
    > MyClass()
    > : a(10), b(20)
    > {
    > a = 10;
    > b = 20;
    > }
    >
    > MyClass(int a, int b)
    > : a(a), b(b)
    > {
    > MyClass();
    > }
    >
    > };
    >
    > All the statement "MyClass();" in the second constructor is create a new
    > default initialised MyClass. So you can call another MyClass
    > constructor, but that creates a new instance. It most definitely is not
    > the same as this->MyClass();
    >
    > If we were to write
    >
    > MyClass(int a, int b)
    > : a(a), b(b)
    > {
    > this->MyClass();
    > }
    >
    > We would have a syntax error.
    >
    > > the result will be _a = 10, _b = 20 even you create an instance by
    > > calling

    >
    > > MyClass * _pClass = new MyClass(100, 100);

    >
    > No, it will not.
    >
    > --
    > Ian Collins.


    Oh really?

    Thanks for that, I've just got back from C# world... my bad
    I apologize.

    cheers,
    Alexander Dong Back Kim, Apr 15, 2008
    #9
  10. Warren Tang

    James Kanze Guest

    Re: Does C++ allow a constructor to call another constructor of thesame class?

    On Apr 15, 4:32 am, Ian Collins <> wrote:
    > Warren Tang wrote:


    > > Does C++ allow a constructor to call another constructor of
    > > the same class?


    > No, if you have common initialisation code, it can go in a
    > member function.


    More precisely, not yet. A proposal for this has been accepted
    for the next version of C++.

    Note that in this case, the call 1) must be in the
    initialization list (and not in the body of the constructor),
    and 2) once the first constructor has finished, the object is
    considered "completely constructed", i.e.:

    C::C( ... )
    : member1( ... )
    , member2( ... )
    {
    throw something ; // will not call destructor...
    }

    c::C( ... )
    : C( transformed .. )
    {
    throw something ; // will call destructor...
    }

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 15, 2008
    #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. Matt Graham
    Replies:
    3
    Views:
    500
    Matt Graham
    Feb 7, 2004
  2. Jeff
    Replies:
    2
    Views:
    924
    clintonG
    Sep 19, 2006
  3. Andy
    Replies:
    9
    Views:
    493
    Pete Becker
    Sep 24, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,191
  5. Ryan Taylor
    Replies:
    1
    Views:
    668
    Ryan Taylor
    Sep 9, 2004
Loading...

Share This Page