object memeber initialization

J

josh

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?
 
S

Sumit Rajan

josh said:
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.
 
C

Chris Theis

josh said:
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
 
D

Dave Rahardja

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
 
V

Victor Bazarov

Dave said:
[..]
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
 
D

Dave Rahardja

Dave said:
[..]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top