Re: How can a constructor call another contructor?

Discussion in 'C++' started by jeffc, Aug 26, 2003.

  1. jeffc

    jeffc Guest

    "Josephine Schafer" <> wrote in message
    news:bievrh$8ctc0$-berlin.de...
    >
    > "sbmoon" <> wrote in message
    > news:...
    > > There are two constructors on a class.
    > >
    > > How can a constructor call another constructor?
    > >
    > > Thx in advance!

    >
    > If you are asking if you can call a constructor from another constructor
    > then the answer is No.
    > A constructor denotes an object creation in a ceratin manner.
    > You cannot invoke another constructor within the constructor, which would
    > otherwise denote another object creation
    > while creating the object.


    In the context of the OP's question ("two constructors on a class"), that's
    true. In that case, the normal thing to do is put common code in an
    "initialize" routine. e.g.

    class A
    {
    public:
    A();
    A(int);
    private:
    initialize();
    };

    In both constructors, you can call initialize() to do common work.

    On the other hand, when inheritance or composition are involved, you can of
    course "call" a constructor in the initializer list.
    jeffc, Aug 26, 2003
    #1
    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. E. Robert Tisdale
    Replies:
    2
    Views:
    648
    jeffc
    Aug 26, 2003
  2. Matt Graham
    Replies:
    3
    Views:
    513
    Matt Graham
    Feb 7, 2004
  3. Andy
    Replies:
    9
    Views:
    510
    Pete Becker
    Sep 24, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,230
  5. Warren Tang
    Replies:
    9
    Views:
    670
    James Kanze
    Apr 15, 2008
Loading...

Share This Page