Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > object memeber initialization

Reply
Thread Tools

object memeber initialization

 
 
josh
Guest
Posts: n/a
 
      02-12-2007
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?

 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      02-12-2007
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-lit....html#faq-10.6

Regards,
Sumit.
 
Reply With Quote
 
 
 
 
Chris Theis
Guest
Posts: n/a
 
      02-12-2007
"josh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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-lit....html#faq-10.6

for a more detailed explanation.

Cheers
Chris


 
Reply With Quote
 
Dave Rahardja
Guest
Posts: n/a
 
      02-12-2007
On 12 Feb 2007 03:59:34 -0800, "josh" <(E-Mail Removed)> 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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-12-2007
Dave Rahardja wrote:
> On 12 Feb 2007 03:59:34 -0800, "josh" <(E-Mail Removed)> 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


 
Reply With Quote
 
Dave Rahardja
Guest
Posts: n/a
 
      02-13-2007
On Mon, 12 Feb 2007 09:47:05 -0500, "Victor Bazarov" <(E-Mail Removed)>
wrote:

>Dave Rahardja wrote:
>> On 12 Feb 2007 03:59:34 -0800, "josh" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling memeber function pointer witha constant object. puzzlecracker C++ 3 03-11-2009 08:10 PM
Connection String of Default Memeber Store Ryan ASP .Net 0 02-13-2007 11:47 PM
static class local variable in memeber function john C++ 5 08-12-2005 09:25 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments