473,507 Members | 8,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

composition/aggregation: would like to use constructor body rather than initializer list

I am relatively new to C++ and hope that this question is relevant. I
have spent some time at
the local library and some time on dejanews, but have no decided to go
ahead with my question,
since I found no satisfactory answer yet.

It is about composed/aggregated classes. I am developing a scientific
code (Monte Carlo) in
which I find it natural to have classes with several levels of
aggregation.

I am particularly keen on having members in my classes declared as
pointers to classes.
For the large memory requirements I have, this makes sense, since the
actual information
about class sizes presumably comes from some input class/file.

I code this such that the top-class constructor recursively calls the
constructors of its member classes
in the initializer list and this seems very awkward to me.

To take a concrete example directly from the web:

// User.h
class PointerMember;
class RefParam;

class User{
public:
User( const RefParam &inParam );
virtual ~User()

private:
PointerMember *mPointerMember;
};

// User.cpp
#include "User.h"
User::User( const RefParam &inParam )
: mPointerMember( new PointerMember( inParam ) )
{
return;
}

User::~User()
{
delete mPointerMember;
return;
}
I would much rather do the following:

User::User( const RefParam &inParam )
{
mPointerMember = new PointerMember( inParam ); // DON'T DO THIS
return;
}

i.e. I would love to use the constructor body, rather than the
initialization list.

To me this seems natural, as I might like to perform some branching in
the constructor
or get some non-trivial input info, before building member
mPointerMember - operations
that just don't fit the initializer list.

Now, someone in another thread pointed out that another way to proceed
in order to salvage
the initializer list, would be to build a mPointerMember outside
(before calling) the constructor User(....) and pass it as a
reference to User(.....), which then passes it on to a copy
constructor
of the member mPointerMember. However, isn't this rather an awkward
thing to do? Why should
I even think of defining member contents/size outside of their
belonging class.

To make a long story short. May I actually legally use the constructor
body as not suggested
in the above (DON'T DO THIS)?

If not, then where is my reasoning/design wrong?

Thanks for your help,

Chris
Jul 22 '05 #1
1 2476
"Chris K" <ck******@hotmail.com> wrote...
[...]
It is about composed/aggregated classes. I am developing a scientific
code (Monte Carlo) in
which I find it natural to have classes with several levels of
aggregation.

I am particularly keen on having members in my classes declared as
pointers to classes.
For the large memory requirements I have, this makes sense, since the
actual information
about class sizes presumably comes from some input class/file.
This is not how things are in C++. Class sizes are always the same in C++.
Just like object sizes. They are pre-determined by the class definition.
However, objects are often created of a derived type and pointers to a base
type are used to refer to them (what is known as polymorphism), thus making
objects behave differently based on their "true nature".

I code this such that the top-class constructor recursively calls the
constructors of its member classes
in the initializer list and this seems very awkward to me.
Again, not a correct statement. The "top-class" constructor does invoke
the other constructor but there is nothing _recursive_ about it. The
two classes are different.

To take a concrete example directly from the web:
How about your own example?

// User.h
class PointerMember;
class RefParam;

class User{
public:
User( const RefParam &inParam );
virtual ~User()

private:
PointerMember *mPointerMember;
};

// User.cpp
#include "User.h"
User::User( const RefParam &inParam )
: mPointerMember( new PointerMember( inParam ) )
{
return;
}

User::~User()
{
delete mPointerMember;
return;
}
I would much rather do the following:

User::User( const RefParam &inParam )
{
mPointerMember = new PointerMember( inParam ); // DON'T DO THIS
return;
}

i.e. I would love to use the constructor body, rather than the
initialization list.
If you _love_ it, do it. There is nothing to stop you.

To me this seems natural, as I might like to perform some branching in
the constructor
or get some non-trivial input info, before building member
mPointerMember - operations
that just don't fit the initializer list.
Well, that's good enough a reason for me. If you need to do branching,
and don't want to do it in the initialiser list (rather awkward to use
the ternary operator for that sometimes), write it as you have here.

Now, someone in another thread pointed out that another way to proceed
in order to salvage
the initializer list, would be to build a mPointerMember outside
(before calling) the constructor User(....) and pass it as a
reference to User(.....), which then passes it on to a copy
constructor
of the member mPointerMember.
If you are referring to some other post, wouldn't it be better to quote
it instead of re-telling the story with your own words?
However, isn't this rather an awkward
thing to do? Why should
I even think of defining member contents/size outside of their
belonging class.
Not sure what you mean here, sorry.

To make a long story short. May I actually legally use the constructor
body as not suggested
in the above (DON'T DO THIS)?
Of course. Any time you take some code off a web site, just strip all the
comments so you don't see those "DON'T DO THIS" warnings, and the life is
going to get much easier.
If not, then where is my reasoning/design wrong?


Well, as I pointed out, you have made at least a couple of statements that
suggest your reasoning may not be as straight as you'd like it. Beyond
that... You should probably try to separate several aspects of your design
that are not related and then ask more particular questions on each of
them about which you have a doubt.

Victor
Jul 22 '05 #2

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

Similar topics

11
2391
by: Christopher Benson-Manica | last post by:
(if this is a FAQ, I apologize - I didn't see it) I have the following class declaration: class TMSViewDayList : public TMSViewDayListBase { public: uint start; uint end; TMSViewDayList() :...
34
3057
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
2
5988
by: ccs | last post by:
First, no compiling error for the following code... class CStudent { int id; public: CStudent(int i) : id(i); }; class CTeam
4
10954
by: cmrchs | last post by:
Hi, how do I implement aggregation and how composition in C# ? When I say : an Airplane has a Pilot then I use aggregation but when I say : an Airplane has a Cockpit then I use composition. How...
4
14437
by: Frederik Vanderhaegen | last post by:
Hi, Can anyone explain me the difference between aggregation and composition? I know that they both are "whole-part" relationships and that composition parts are destroyed when the composition...
11
3795
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
2
14808
by: Gary Wessle | last post by:
Hi what is the difference "in code writing" between aggregation and composition? is the following correct? aggregation (life time of both objects are independent). class A{ /* ... */ };...
4
5490
by: fireball | last post by:
hi, I got confused for a moment about creating data structure for UML composition (strong aggregation) relation one-to-many. I used Rose/DataModeler to do so. <filled_diamond>-------- I...
8
2424
by: Jess | last post by:
Hello, When I define default constructors, I tend to use constructor initializers for member data. However, I was told the order in which members are initialized is determined by the order of...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
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
7372
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...
1
7029
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
7481
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...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.