java like constructor calling constructor

Discussion in 'C++' started by lallous, Jan 23, 2004.

  1. lallous

    lallous Guest

    Hello,

    Probably I am not seeing it now, but is it possible for constructor(int) to
    call constructor() ?

    class A
    {
    private:
    int x, y;
    public:
    A();
    A(int);
    };

    A::A()
    {
    x = y = 0;
    }

    A::A(int x)
    {
    // call A::A() ?
    // in Java, it is: this()
    }

    Or I have to write a small function like:
    A::ctor()
    {
    // do common stuff here
    }
    A::A()
    {
    ctor();
    }
    A::A(int x)
    {
    ctor();
    // more stuff here
    }


    --
    Elias
     
    lallous, Jan 23, 2004
    #1
    1. Advertising

  2. lallous

    Attila Feher Guest

    lallous wrote:
    > Hello,
    >
    > Probably I am not seeing it now, but is it possible for
    > constructor(int) to call constructor() ?

    [SNIP]

    No. It is being discussed as a possible future feature (delegating
    constructor???) but right now all you can do is to make a 3rd (named)
    function and call that one from both constructors. This - of course - lacks
    the capability of sharing initializer lists.

    --
    Attila aka WW
     
    Attila Feher, Jan 23, 2004
    #2
    1. Advertising

  3. "lallous" <> wrote...
    > Probably I am not seeing it now, but is it possible for constructor(int)

    to
    > call constructor() ?


    No. Constructors don't have names, they cannot be called.

    > [...]
    >
    > Or I have to write a small function like:
    > A::ctor()


    Yes.

    V
     
    Victor Bazarov, Jan 23, 2004
    #3
  4. lallous

    Rolf Magnus Guest

    lallous wrote:

    > Hello,
    >
    > Probably I am not seeing it now, but is it possible for
    > constructor(int) to call constructor() ?


    No.

    > Or I have to write a small function like:


    Either that, or use default values for the parameters of your A(int)
    constructor.
     
    Rolf Magnus, Jan 23, 2004
    #4
  5. On Fri, 23 Jan 2004 16:41:57 +0200, Attila Feher wrote:

    > lallous wrote:
    >> Hello,
    >>
    >> Probably I am not seeing it now, but is it possible for
    >> constructor(int) to call constructor() ?

    > [SNIP]
    >
    > No. It is being discussed as a possible future feature (delegating
    > constructor???) but right now all you can do is to make a 3rd (named)
    > function and call that one from both constructors. This - of course - lacks
    > the capability of sharing initializer lists.


    Well another thing that sometimes works is creating a baseclass that has
    the common code in its constructor.

    HTH,
    M4
     
    Martijn Lievaart, Jan 23, 2004
    #5
  6. lallous

    David Harmon Guest

    On Fri, 23 Jan 2004 16:32:55 +0200 in comp.lang.c++, "lallous"
    <> was alleged to have written:
    >is it possible for constructor(int) to call constructor() ?


    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, Jan 23, 2004
    #6
    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,067
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,350
    John Harrison
    Jun 29, 2003
  3. Asfand Yar Qazi
    Replies:
    6
    Views:
    15,713
    jeffc
    May 17, 2004
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    500
    Patrick Kowalzick
    Mar 14, 2006
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,341
Loading...

Share This Page