473,395 Members | 1,466 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,395 software developers and data experts.

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?

Nov 15 '06 #1
3 1526

josh äæÔÊå ÇÓÊ:
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

Nov 15 '06 #2
s_*********@yahoo.com wrote:
josh äæÔÊå ÇÓÊ:
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

Nov 15 '06 #3
LR
er****@student.chalmers.se wrote:
s_*********@yahoo.com wrote:

>>josh äæÔÊå ÇÓÊ:
>>>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
Nov 15 '06 #4

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

Similar topics

4
by: Avner Flesch | last post by:
Hi, Do you know how can I intialize an array member: for example class A { public: A(int x); };
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
3
by: Tran Tuan Anh | last post by:
Dear all, I am new with C++ and very confused with some features. Really appreciate if you can explain to me some of stuffs below. I define a class: class A { static A* instance = 0; };
10
by: emma middlebrook | last post by:
Hi I discovered that if you declare a structure (and not 'new()' it) you can then separately initialize its members and the compiler counts those separate statements as a full initialization....
11
by: asdf | last post by:
The oder of member initialization is the order in which the members are defined. So the following code is problematic: class X{ int i; int j; public: X(int val):j(val),i(j){}
7
by: Spoon | last post by:
Hello everyone, I have a Packet class I use to send packets over the Internet. All the packets sent in a session are supposed to share a common random ID. I figured I'd use a static const...
5
by: Bryan | last post by:
Hi, Where is the proper place to use a member initialization list, the header or cpp file? Does it make any difference? Also, is there any difference between using a member initialization...
10
by: Dennis Jones | last post by:
Hello, I have a hierarchy of classes in which there will be a data element that is common to all descendant classes. This element (a string in this case) is constant, but specific to each...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.