object memeber initialization

Discussion in 'C++' started by josh, Feb 12, 2007.

  1. josh

    josh Guest

    Hi all I've some doubts about the vary way to initialize an object
    inside a class:

    If I have an object of class B inside a Class A then to init B if it's
    a ponter
    a reference
    a simple object and so on

    I've seen (where objectB is B objectB or B *objectB) sintax like

    A::A : objectB()

    or

    A::A()
    {
    objectB = B();
    }

    or

    A::A : objectB( new B() )

    or
    A::A()
    {
    objectB = new B();
    }

    so which are the BEST way to accomplish that?
    josh, Feb 12, 2007
    #1
    1. Advertising

  2. josh

    Sumit Rajan Guest

    josh wrote:
    > Hi all I've some doubts about the vary way to initialize an object
    > inside a class:
    >
    > If I have an object of class B inside a Class A then to init B if it's
    > a ponter
    > a reference
    > a simple object and so on
    >
    > I've seen (where objectB is B objectB or B *objectB) sintax like
    >
    > A::A : objectB()
    >
    > or
    >
    > A::A()
    > {
    > objectB = B();
    > }
    >
    > or
    >
    > A::A : objectB( new B() )
    >
    > or
    > A::A()
    > {
    > objectB = new B();
    > }
    >
    > so which are the BEST way to accomplish that?
    >



    See:
    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.6

    Regards,
    Sumit.
    Sumit Rajan, Feb 12, 2007
    #2
    1. Advertising

  3. josh

    Chris Theis Guest

    "josh" <> wrote in message
    news:...
    > Hi all I've some doubts about the vary way to initialize an object
    > inside a class:
    >
    > If I have an object of class B inside a Class A then to init B if it's
    > a ponter
    > a reference
    > a simple object and so on
    >
    > I've seen (where objectB is B objectB or B *objectB) sintax like
    >
    > A::A : objectB()
    >
    > or
    >
    > A::A()
    > {
    > objectB = B();
    > }
    >
    > or
    >
    > A::A : objectB( new B() )
    >
    > or
    > A::A()
    > {
    > objectB = new B();
    > }
    >
    > so which are the BEST way to accomplish that?


    Usually your ctor should use initializer lists instead of assignments. But
    I'd recommend to take a look at the FAQ

    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.6

    for a more detailed explanation.

    Cheers
    Chris
    Chris Theis, Feb 12, 2007
    #3
  4. On 12 Feb 2007 03:59:34 -0800, "josh" <> wrote:

    >Hi all I've some doubts about the vary way to initialize an object
    >inside a class:
    >
    >If I have an object of class B inside a Class A then to init B if it's
    >a ponter
    >a reference
    >a simple object and so on
    >
    > I've seen (where objectB is B objectB or B *objectB) sintax like
    >
    >A::A : objectB()
    >
    >or
    >
    >A::A()
    >{
    > objectB = B();
    >}
    >
    >or
    >
    >A::A : objectB( new B() )
    >
    >or
    >A::A()
    >{
    > objectB = new B();
    >}
    >
    >so which are the BEST way to accomplish that?


    Almost always the constructor initializer list is the way to go. It allows you
    to do things like initialize const member variables, and initialize member
    variables that do not have default (no parameter) constructors.

    -dr
    Dave Rahardja, Feb 12, 2007
    #4
  5. Dave Rahardja wrote:
    > On 12 Feb 2007 03:59:34 -0800, "josh" <> wrote:
    >> [..]
    >> so which are the BEST way to accomplish that?

    >
    > Almost always the constructor initializer list is the way to go. It
    > allows you to do things like initialize const member variables, and
    > initialize member variables that do not have default (no parameter)
    > constructors.


    Just a nit-pick: a default c-tor is not the one that has no arguments
    (parameters). It's the one that can be used without specifying any.
    Compare

    class NoArgCtor {
    public:
    NoArgCtor(); // the default c-tor
    };

    class HasDefaultCtor {
    public:
    HasDefaultCtor(int = 42); // _also_ the default c-tor
    };

    int main() {
    NoArgCtor na;
    HasDefaultCtor hd; // same as "hd(42)"
    }

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 12, 2007
    #5
  6. On Mon, 12 Feb 2007 09:47:05 -0500, "Victor Bazarov" <>
    wrote:

    >Dave Rahardja wrote:
    >> On 12 Feb 2007 03:59:34 -0800, "josh" <> wrote:
    >>> [..]
    >>> so which are the BEST way to accomplish that?

    >>
    >> Almost always the constructor initializer list is the way to go. It
    >> allows you to do things like initialize const member variables, and
    >> initialize member variables that do not have default (no parameter)
    >> constructors.

    >
    >Just a nit-pick: a default c-tor is not the one that has no arguments
    >(parameters). It's the one that can be used without specifying any.
    >Compare
    >
    > class NoArgCtor {
    > public:
    > NoArgCtor(); // the default c-tor
    > };
    >
    > class HasDefaultCtor {
    > public:
    > HasDefaultCtor(int = 42); // _also_ the default c-tor
    > };
    >
    > int main() {
    > NoArgCtor na;
    > HasDefaultCtor hd; // same as "hd(42)"
    > }
    >
    >V


    GAH! You got me.

    -dr
    Dave Rahardja, Feb 13, 2007
    #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. JKop
    Replies:
    10
    Views:
    941
  2. Ryan
    Replies:
    0
    Views:
    321
  3. Matthias Kaeppler
    Replies:
    2
    Views:
    440
    Victor Bazarov
    Jul 18, 2005
  4. john
    Replies:
    5
    Views:
    280
    Victor Bazarov
    Aug 12, 2005
  5. puzzlecracker
    Replies:
    3
    Views:
    803
    Andrey Tarasevich
    Mar 11, 2009
Loading...

Share This Page