Connecting Tech Pros Worldwide Help | Site Map

member initialization

  #1  
Old November 15th, 2006, 10:05 AM
josh
Guest
 
Posts: n/a
Hi,

if I've:

class Date
{
public:
Date(int m, int d, int y);
}

Date::Date(int m, int d, int y)
{
....
}


and then a class in which I've another object:
class A
{
public:
A(int, int, int);

private:
Date d;
}
A::A(int m, int d, int y) : d(m, d, y)
{
...
}
above is the only method to initialize objects?
I can't do:
A::A(int m, int d, int y)
{
d(m, d, y);
}

Correct?

  #2  
Old November 15th, 2006, 11:55 AM
s_amrollahi@yahoo.com
Guest
 
Posts: n/a

re: member initialization



josh نوشته است:
Quote:
Hi,
>
if I've:
>
class Date
{
public:
Date(int m, int d, int y);
}
>
Date::Date(int m, int d, int y)
{
...
}
>
>
and then a class in which I've another object:
class A
{
public:
A(int, int, int);
>
private:
Date d;
}
A::A(int m, int d, int y) : d(m, d, y)
{
..
}
above is the only method to initialize objects?
I can't do:
A::A(int m, int d, int y)
{
d(m, d, y);
}
>
Correct?
Yes. Because Date has no Default Coonstructor, So it should be
initialized using member initialization list in A's constructor.

Regards

  #3  
Old November 15th, 2006, 12:45 PM
eriwik@student.chalmers.se
Guest
 
Posts: n/a

re: member initialization


s_amrollahi@yahoo.com wrote:
Quote:
josh نوشته است:
Quote:
Hi,

if I've:

class Date
{
public:
Date(int m, int d, int y);
}

Date::Date(int m, int d, int y)
{
...
}


and then a class in which I've another object:
class A
{
public:
A(int, int, int);

private:
Date d;
}
A::A(int m, int d, int y) : d(m, d, y)
{
..
}
above is the only method to initialize objects?
I can't do:
A::A(int m, int d, int y)
{
d(m, d, y);
}

Correct?
Yes. Because Date has no Default Coonstructor, So it should be
initialized using member initialization list in A's constructor.
Yes, and even if you did have a default constructor it would still be
the best way to initialize the object, since if you didn't a
Date-object would first be created and then replaced with the one with
the right date set. Using an initializer-list you only create one
Date-object.

--
Erik Wikstrِm

  #4  
Old November 15th, 2006, 02:35 PM
LR
Guest
 
Posts: n/a

re: member initialization


eriwik@student.chalmers.se wrote:
Quote:
s_amrollahi@yahoo.com wrote:
>
>
Quote:
>>josh نوشته است:
>>
Quote:
>>>Hi,
>>>
>>>if I've:
>>>
>>>class Date
>>>{
>> public:
>> Date(int m, int d, int y);
>>>}
>>>
>>>Date::Date(int m, int d, int y)
>>>{
>>>...
>>>}
>>>
>>>
>>>and then a class in which I've another object:
>>>class A
>>>{
>>>public:
>> A(int, int, int);
>>>
>>>private:
>> Date d;
>>>}
>>>A::A(int m, int d, int y) : d(m, d, y)
>>>{
>>>..
>>>}
>>>above is the only method to initialize objects?
>>>I can't do:
>>>A::A(int m, int d, int y)
>>>{
>> d(m, d, y);
>>>}
>>>
>>>Correct?
>>
>>Yes. Because Date has no Default Coonstructor, So it should be
>>initialized using member initialization list in A's constructor.
>
>
Yes, and even if you did have a default constructor it would still be
the best way to initialize the object, since if you didn't a
Date-object would first be created and then replaced with the one with
the right date set. Using an initializer-list you only create one
Date-object.
So efficiency is more important then, say readability? Because I'd find
this a little bit clearer

A a(Date(11,15,2006));

then this:

A a(11,15,2006);

Of course, I'm supposing that Date has a copy ctor, and A has a ctor
that looks like:

A::A(const Date &dateArgument) : d(dateArgument) {}

I think this also has the advantage of being a little more flexible if
for some reason Date gets new ctors. After all, you have a Date that is
a member of A, why expose, even as data initializers, the component
members of Date in A's ctor?


And even if Date didn't have a copy ctor, but had getters for m, d and y
(month, day and year might be better names?) I would still think that
having a ctor in A that looked like this would be clearer (even if it
was a lot less efficient):

A::A(const Date &dateArgument)
:
d(dateArgument.month(), dateArgument.day(), dateArgument.year())
{}

But then, your application and YMWV.


LR
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Member initialization list question Bryan answers 5 January 10th, 2007 07:25 PM
static const member initialization Spoon answers 7 December 31st, 2006 03:45 PM
The order of member initialization asdf answers 11 October 3rd, 2006 07:55 PM
Namespace/Template member initialization issue Brian Ross answers 2 November 16th, 2005 09:25 PM
Namespace/Template member initialization issue Brian Ross answers 3 July 19th, 2005 05:27 PM