Building constructors with shared functionality

Discussion in 'C++' started by Aguilar, James, Jul 12, 2004.

  1. I want to call a constructor from another constructor without constructing
    an object. In other words, I want to partially initialize the object I'm
    working with with one constructor, and finish the job with another.

    class Foo
    {
    public:
    Foo();
    Foo(int i);
    private:
    int a, b;
    };

    Foo::Foo()
    {
    a = b = 0;
    }

    Foo::Foo(int i)
    {
    Foo(); //or should it be Foo::Foo()?
    a = i;
    }

    Is there any way to do this in C++ that does not involve private helper
    methods?
     
    Aguilar, James, Jul 12, 2004
    #1
    1. Advertising

  2. On Mon, 12 Jul 2004 03:05:14 -0400, Aguilar, James
    <> wrote:

    > I want to call a constructor from another constructor without
    > constructing
    > an object. In other words, I want to partially initialize the object I'm
    > working with with one constructor, and finish the job with another.
    >
    > class Foo
    > {
    > public:
    > Foo();
    > Foo(int i);
    > private:
    > int a, b;
    > };
    >
    > Foo::Foo()
    > {
    > a = b = 0;
    > }
    >
    > Foo::Foo(int i)
    > {
    > Foo(); //or should it be Foo::Foo()?
    > a = i;
    > }
    >
    > Is there any way to do this in C++ that does not involve private helper
    > methods?
    >


    No. But if you really need a partially initialised object that might be a
    clue that what you really need is to split your object into a base class
    and a derived class. While the derived class is being created the base
    class will be created first and that is your 'partially initialised
    object'.

    john
     
    John Harrison, Jul 12, 2004
    #2
    1. Advertising

  3. Aguilar, James

    Sharad Kala Guest

    "Aguilar, James" <> wrote in message
    news:cctd78$o9c$...
    > I want to call a constructor from another constructor without constructing
    > an object. In other words, I want to partially initialize the object I'm
    > working with with one constructor, and finish the job with another.
    >
    > class Foo
    > {
    > public:
    > Foo();
    > Foo(int i);
    > private:
    > int a, b;
    > };
    >
    > Foo::Foo()
    > {
    > a = b = 0;
    > }
    >
    > Foo::Foo(int i)
    > {
    > Foo(); //or should it be Foo::Foo()?
    > a = i;
    > }


    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.3
    You can't call a constructor. Foo() in your above definition of Foo(int i)
    is a temporary object, it doesn't call the constructor. Instead write some
    private member function init and call it.
    -Sharad
     
    Sharad Kala, Jul 12, 2004
    #3
  4. * Sharad Kala:
    >
    > http://www.parashift.com/c -faq-lite/ctors.html#faq-10.3
    > You can't call a constructor


    .... on the 'this' object (without using placement new, which you
    shouldn't anyway).

    Read the FAQ and HS carefully.


    >. Foo() in your above definition of Foo(int i)
    > is a temporary object, it doesn't call the constructor. Instead write some
    > private member function init and call it.


    Right.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jul 12, 2004
    #4
  5. Aguilar, James

    Sharad Kala Guest

    "Alf P. Steinbach" <> wrote in message
    news:...
    > * Sharad Kala:
    > >
    > > http://www.parashift.com/c -faq-lite/ctors.html#faq-10.3
    > > You can't call a constructor

    >
    > ... on the 'this' object (without using placement new, which you
    > shouldn't anyway).


    >
    > Read the FAQ and HS carefully.


    Well, I have :) I shouldn't have been in a haste though.
     
    Sharad Kala, Jul 12, 2004
    #5
    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. Dave Rudolf
    Replies:
    12
    Views:
    8,306
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    591
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    610
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    400
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    472
Loading...

Share This Page