473,320 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

object memeber initialization

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?

Feb 12 '07 #1
5 1123
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.
Feb 12 '07 #2
"josh" <xd********@yahoo.comwrote in message
news:11**********************@p10g2000cwp.googlegr 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
Feb 12 '07 #3
On 12 Feb 2007 03:59:34 -0800, "josh" <xd********@yahoo.comwrote:
>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
Feb 12 '07 #4
Dave Rahardja wrote:
On 12 Feb 2007 03:59:34 -0800, "josh" <xd********@yahoo.comwrote:
>[..]
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
Feb 12 '07 #5
On Mon, 12 Feb 2007 09:47:05 -0500, "Victor Bazarov" <v.********@comAcast.net>
wrote:
>Dave Rahardja wrote:
>On 12 Feb 2007 03:59:34 -0800, "josh" <xd********@yahoo.comwrote:
>>[..]
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
Feb 13 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
8
by: Baloff | last post by:
Hello I am not sure why my compiler will not initialize string e1("sam"); and will initialize string e1 = "sam"; here is my code and the error. thanks alot
7
by: xllx.relient.xllx | last post by:
Q1: What happens when you apply parenthesis to the class type name? Is the constructor for the class called and returns an object?, or what? in main...: MyClass(); // end
8
by: sarathy | last post by:
Hi, I read the following points in K&R "Section A8.7 Initialization". Seems like i have a problem with them. * All expressions in the initialization of constant object/array must be constant...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
5
by: kiryazev | last post by:
Hello. Given the code below does C++ Standard guarantee that the function my_init() will be called before main()? struct A { A() { my_init(); } };
4
by: .rhavin grobert | last post by:
guess you have a bouquet of paddingless structs (and your compiler already cares for that) that all have one in common: their first memeber has to be their size. As fas as i know (am i right?) a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.