Calling another constructor from a constructor

Discussion in 'C++' started by Asfand Yar Qazi, May 14, 2004.

  1. Hi,

    Consider this:

    class C
    {
    int j;
    public:
    C(int i)
    : j(i)
    {
    }

    C()
    {
    //C::C(0)
    // this is obviously wrong, but is
    // something like this possible?

    *this = C(0);
    }

    }; // class C

    Is there any way of calling another constructor from a
    constructor to initialize your object? I know about using the
    assignment operator as a go-between as above, but is there some
    other more efficient way?

    Thanks,
    Asfand Yar


    --
    http://www.it-is-truth.org/
    Asfand Yar Qazi, May 14, 2004
    #1
    1. Advertising

  2. Asfand Yar Qazi

    Buboi Peng Guest

    static void InitFunc(int n)
    {
    ...
    }

    class C{
    friend void InitFunc();
    public:
    C(int n){ InitFunc(n); }
    C() { InitFunc(0); }
    };

    --
    Buboi
    =============================
    ??????????


    "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
    news:c81ps3$tbg$...
    > Hi,
    >
    > Consider this:
    >
    > class C
    > {
    > int j;
    > public:
    > C(int i)
    > : j(i)
    > {
    > }
    >
    > C()
    > {
    > //C::C(0)
    > // this is obviously wrong, but is
    > // something like this possible?
    >
    > *this = C(0);
    > }
    >
    > }; // class C
    >
    > Is there any way of calling another constructor from a
    > constructor to initialize your object? I know about using the
    > assignment operator as a go-between as above, but is there some
    > other more efficient way?
    >
    > Thanks,
    > Asfand Yar
    >
    >
    > --
    > http://www.it-is-truth.org/
    Buboi Peng, May 14, 2004
    #2
    1. Advertising

  3. Asfand Yar Qazi

    Siemel Naran Guest

    "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
    news:c81ps3

    > class C
    > {
    > int j;
    > public:
    > C(int i)
    > : j(i)
    > {
    > }
    >
    > C()
    > {
    > //C::C(0)
    > // this is obviously wrong, but is
    > // something like this possible?
    >
    > *this = C(0);
    > }
    >
    > }; // class C


    It's a nice feature to have, one constructor forwards to another.

    The usual ideas are:

    (1) Use default parameters. In your case

    C(int i=0)

    (2) Have the constructors call one init function. But if you have user
    types, this means you call the default constructor for each in the
    (non-existent) member initialization list, though this might be OK for all
    practical purposes.

    (3) Define a nested class, and have your class contain an instance of this
    class. The nested class constructor is the init function of method (1).

    class C
    {
    struct Imp { int j; Imp(int j); } imp;
    public:
    C(int i) : imp(i) { }
    C() : imp(0) { }
    };
    Siemel Naran, May 14, 2004
    #3
  4. Buboi Peng wrote:
    > static void InitFunc(int n)
    > {
    > ...
    > }
    >
    > class C{
    > friend void InitFunc();
    > public:
    > C(int n){ InitFunc(n); }
    > C() { InitFunc(0); }
    > };
    >


    I did something like this in the end.

    Thanks to all.



    --
    http://www.it-is-truth.org/
    Asfand Yar Qazi, May 14, 2004
    #4
  5. Asfand Yar Qazi

    Christian Guest

    Asfand Yar Qazi <im_not_giving_it_here@i_hate_spam.com> wrote in message news:<c81ps3$tbg$>...
    > Hi,
    >
    > Consider this:
    >
    > class C
    > {
    > int j;
    > public:
    > C(int i)
    > : j(i)
    > {
    > }
    >
    > C()
    > {
    > //C::C(0)
    > // this is obviously wrong, but is
    > // something like this possible?
    >
    > *this = C(0);
    > }
    >
    > }; // class C
    >
    > Is there any way of calling another constructor from a
    > constructor to initialize your object?


    No. But as another poster suggested, you might use some sort of "init"
    function to call from both.

    > I know about using the
    > assignment operator as a go-between as above, but is there some
    > other more efficient way?


    For your particular example, why not simply use a single constructor
    with a default argument (defaulting to 0 in your case)?

    Cheers,
    Christian

    >
    > Thanks,
    > Asfand Yar
    Christian, May 14, 2004
    #5
  6. Asfand Yar Qazi

    David Harmon Guest

    On Fri, 14 May 2004 07:49:25 +0100 in comp.lang.c++, Asfand Yar Qazi
    <im_not_giving_it_here@i_hate_spam.com> wrote,
    >Is there any way of calling another constructor from a
    >constructor to initialize your object?


    This issue is covered in Marshall Cline's C++ FAQ. See the topic
    "[10.3] Can one constructor of a class call another constructor of the
    same class to initialize the this object?" It is always good to check
    the FAQ before posting. You can get the FAQ at:
    http://www.parashift.com/c -faq-lite/
    David Harmon, May 14, 2004
    #6
  7. Asfand Yar Qazi

    jeffc Guest

    "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
    news:c81ps3$tbg$...
    >
    > Is there any way of calling another constructor from a
    > constructor to initialize your object?


    No. You can just create an "initialize" method that they both call.
    jeffc, May 17, 2004
    #7
    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. Giulio
    Replies:
    9
    Views:
    1,043
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,335
    John Harrison
    Jun 29, 2003
  3. Neroku
    Replies:
    8
    Views:
    1,198
    Tor Iver Wilhelmsen
    Nov 18, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,229
  5. Alf P. Steinbach
    Replies:
    4
    Views:
    410
    James Kanze
    Nov 30, 2007
Loading...

Share This Page