Connecting Tech Pros Worldwide Help | Site Map

member initialization

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 15th, 2006, 09:05 AM
josh
Guest
 
Posts: n/a
Default member initialization

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, 10:55 AM
s_amrollahi@yahoo.com
Guest
 
Posts: n/a
Default 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, 11:45 AM
eriwik@student.chalmers.se
Guest
 
Posts: n/a
Default 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, 01:35 PM
LR
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.