another bloody basic question

Discussion in 'C++' started by jesse, Nov 6, 2003.

  1. jesse

    jesse Guest

    In java, one constructor can call another constructor through this(...)

    for instance

    class foo
    {
    public:
    foo(int k) { this(k,false)};
    foo(int k, boolean m){...};
    }

    It seems i can't find similar syntax in c++.
    what is the notion for one constructor to call another?

    sorry for this bloody basic question.

    jesse
    jesse, Nov 6, 2003
    #1
    1. Advertising

  2. Janusz Szpilewski, Nov 6, 2003
    #2
    1. Advertising

  3. jesse wrote:

    > In java, one constructor can call another constructor through this(...)
    >
    > for instance
    >
    > class foo
    > {
    > public:
    > foo(int k) { this(k,false)};
    > foo(int k, boolean m){...};
    > }
    >
    > It seems i can't find similar syntax in c++.
    > what is the notion for one constructor to call another?
    >
    > sorry for this bloody basic question.
    > ...


    There's no such syntax in C++. In some cases the common code can be
    transferred from constructors to a separate function (regular function,
    not a constructor), which then can be called from all constructors. In
    other cases there is no other way but to repeat the same code in all
    constructors.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 6, 2003
    #3
  4. jesse

    tom_usenet Guest

    On Wed, 05 Nov 2003 18:12:54 -0800, jesse <> wrote:

    >In java, one constructor can call another constructor through this(...)
    >
    >for instance
    >
    >class foo
    >{
    >public:
    > foo(int k) { this(k,false)};
    > foo(int k, boolean m){...};
    >}
    >
    >It seems i can't find similar syntax in c++.
    >what is the notion for one constructor to call another?
    >
    >sorry for this bloody basic question.


    Basically you can't. The C++ construction model is very different to
    Javas. By the time you enter the {} part of your constructor, all base
    classes and all member variables have already been constructed. This
    is because of the existence of user-defined value types, a concept
    that Java doesn't have.

    Have you come across initializer lists?

    foo(int k)
    :m_k(k)
    {}

    foo(int k, boolean m)
    :m_k(k), m_m(m)
    {}

    With initializer lists, the constructor body is often empty anyway, so
    what is there to share?

    That said, I think a syntax like this has been proposed at some point
    in the past, but I don't think any compiler supports it:

    foo(int k)
    :foo(k, false)
    {
    //anything extra
    }

    Tom
    tom_usenet, Nov 6, 2003
    #4
  5. On Wed, 05 Nov 2003 18:12:54 -0800, jesse wrote:

    > In java, one constructor can call another constructor through this(...)
    >
    > for instance
    >
    > class foo
    > {
    > public:
    > foo(int k) { this(k,false)};
    > foo(int k, boolean m){...};
    > }
    >
    > It seems i can't find similar syntax in c++.
    > what is the notion for one constructor to call another?


    You can make an init() function that is called from the constructors,
    though for your example the simplest would be:

    class Foo {
    public:
    Foo(int k, bool m = false);
    }

    When you call do new Foo(42) the default is used for m since you didn't
    supply a value.

    --
    NPV

    "the large print giveth, and the small print taketh away"
    Tom Waits - Step right up
    Nils Petter Vaskinn, Nov 6, 2003
    #5
  6. "tom_usenet" <> wrote in message
    news:...
    > On Wed, 05 Nov 2003 18:12:54 -0800, jesse <> wrote:
    >
    > >In java, one constructor can call another constructor through this(...)
    > >
    > >for instance
    > >
    > >class foo
    > >{
    > >public:
    > > foo(int k) { this(k,false)};
    > > foo(int k, boolean m){...};
    > >}
    > >
    > >It seems i can't find similar syntax in c++.
    > >what is the notion for one constructor to call another?
    > >
    > >sorry for this bloody basic question.

    >
    > Basically you can't. The C++ construction model is very different to
    > Javas. By the time you enter the {} part of your constructor, all base
    > classes and all member variables have already been constructed. This
    > is because of the existence of user-defined value types, a concept
    > that Java doesn't have.
    >
    > Have you come across initializer lists?
    >
    > foo(int k)
    > :m_k(k)
    > {}
    >
    > foo(int k, boolean m)
    > :m_k(k), m_m(m)
    > {}
    >
    > With initializer lists, the constructor body is often empty anyway, so
    > what is there to share?


    The equivalent in C++ is to use the initializer list to indicate the
    constructor of the base class to be used. It is selected by the normal
    overloading operation.
    Here is a simple example showing construction of a derived class with no,
    one, or two arguments.

    #include <iostream>
    using namespace std;

    class B
    {public:
    int b;
    B( ):b(2){}
    B(int x):b(x){}
    };
    class A : public B
    {public:
    int a;
    A( ):a(1){}
    A(int x):a(x){}
    A(int x, int y):a(x),B(y){}
    };
    int main ( )
    {
    A myA;
    cout <<myA.a<<myA.b<<endl;
    A myA1(3);
    cout<<myA1.a<<myA1.b<<endl;
    A myA2(4,5);
    cout << myA2.a<<myA2.b<<endl;
    return 0;
    }

    --
    Gary
    Gary Labowitz, Nov 6, 2003
    #6
  7. jesse

    Ron Natalie Guest

    "jesse" <> wrote in message news:...

    > It seems i can't find similar syntax in c++.
    > what is the notion for one constructor to call another?


    There is no similar syntax in C++. You can't call constructors in C++.
    The cleanest work around is to put the common code in a seperate
    member function and call it from both constructors.
    Ron Natalie, Nov 6, 2003
    #7
  8. jesse

    Ron Natalie Guest

    "Andrey Tarasevich" <> wrote in message news:...

    >
    > There's no such syntax in C++. In some cases the common code can be
    > transferred from constructors to a separate function (regular function,
    > not a constructor), which then can be called from all constructors. In
    > other cases there is no other way but to repeat the same code in all
    > constructors.


    There's nothing that ever prevents the constructor body from being
    moved. What's a pain in the butt is that there is no way to share
    the initializer lists between two constructors.
    Ron Natalie, Nov 6, 2003
    #8
    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. berthelot samuel

    this is a bloody hard one (for me...) please help me !

    berthelot samuel, Nov 1, 2003, in forum: C Programming
    Replies:
    6
    Views:
    334
    berthelot samuel
    Nov 2, 2003
  2. Engineer
    Replies:
    6
    Views:
    613
    Jeremy Bowers
    May 1, 2005
  3. President of the United STOOGES

    Bloody Crime Scene Photos Of DEAD Celebrities & Friends

    President of the United STOOGES, Sep 27, 2006, in forum: C++
    Replies:
    0
    Views:
    860
    President of the United STOOGES
    Sep 27, 2006
  4. Marcin Krol
    Replies:
    0
    Views:
    269
    Marcin Krol
    Jul 2, 2008
  5. Rob R. Ainscough
    Replies:
    3
    Views:
    322
    Rob R. Ainscough
    Jun 14, 2005
Loading...

Share This Page