473,395 Members | 1,915 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.

avoiding repeated code in initialization lists

If you have a class with:

1. Many members

2. Two or more constructors

and if you follow the C++ FAQ guidelines of initializing all member
variables in a contructor initialiazation list, then you might have
two constructors that repeat much of the same code:

MyClass() :
_member1(0),
_member2(0),
_member3("default string")
_member4(0.0)
...etc...
{}

MyClass(int x, int y) :
_member1(0),
_member2(0),
_member3("default string")
_member4(0.0)
...etc...
{
// does some stuff with x and y
}

How can you avoid this unnecessary repetition?

Thanks for any ideas,
cpp
Jul 22 '05 #1
3 1062

"cppaddict" <he***@hello.com> wrote in message
news:56********************************@4ax.com...
If you have a class with:

1. Many members

2. Two or more constructors

and if you follow the C++ FAQ guidelines of initializing all member
variables in a contructor initialiazation list, then you might have
two constructors that repeat much of the same code:

MyClass() :
_member1(0),
_member2(0),
_member3("default string")
_member4(0.0)
...etc...
{}

MyClass(int x, int y) :
_member1(0),
_member2(0),
_member3("default string")
_member4(0.0)
...etc...
{
// does some stuff with x and y
}

How can you avoid this unnecessary repetition?

Thanks for any ideas,
cpp


One way is to factor out the common code into another class, usually making
that other class a base class.

class MyClassBase
{
public:
MyClassBase() :
_member1(0),
_member2(0),
_member3("default string")
_member4(0.0)
{
}
protected:
int _member1;
int _member2;
string _member3;
double _member4;
};

class MyClass : public MyClassBase
{
public:
MyClass() {}
MyClass(int x, int y)
{
// do something with x and y
}
};

john
Jul 22 '05 #2
On Sat, 19 Jun 2004 09:22:59 GMT, cppaddict <he***@hello.com> wrote:
If you have a class with:

1. Many members

2. Two or more constructors
[]
How can you avoid this unnecessary repetition?


You might like using boost::value_initialized<>. It takes care of member default initialization.

--
Maxim Yegorushkin
Jul 22 '05 #3
On Sat, 19 Jun 2004 09:22:59 +0000, cppaddict wrote:
If you have a class with:

1. Many members

2. Two or more constructors

and if you follow the C++ FAQ guidelines of initializing all member
variables in a contructor initialiazation list, then you might have
two constructors that repeat much of the same code:

MyClass() :
_member1(0),
_member2(0),
_member3("default string")
_member4(0.0)
...etc...
{}

MyClass(int x, int y) :
_member1(0),
_member2(0),
_member3("default string")
_member4(0.0)
...etc...
{
// does some stuff with x and y
}

How can you avoid this unnecessary repetition?

Thanks for any ideas,
cpp

Instead of overloading the constructors, You might consider using default
parameters. This may or may not be appropriate, depending on how the
class and the constructors are designed ...

Jul 22 '05 #4

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

Similar topics

1
by: Jacek Dziedzic | last post by:
Hi! A) Why isn't it possible to set a member of the BASE class in an initialization list of a DERIVED class constructor (except for 'calling' the base constructor from there, of course)? I even...
15
by: Robert Sturzenegger | last post by:
// Code sequence A for (int i = 0; i < 10; ++i) { int k = something(); // some more code which uses k } // Code sequence B int k; for (int i = 0; i < 10; ++i) { k = something();
2
by: NH | last post by:
Hi, I have two dropdown lists in a web form. When you choose an entry in the first list the code retrieves records from the database and displays them in the second dropdownlist. Is there a...
4
by: Jacek Dziedzic | last post by:
Hello! Suppose I have a class Foo that defines a default c'tor that initializes some data using an initialization list: Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20) // and...
4
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple...
13
by: John Devereux | last post by:
Hi, For a resource constrained embedded system I want to avoid malloc. Currently I have various "library" modules that hide their data behind opaque pointers. Because I read that was a good...
27
by: Cephalobus_alienus | last post by:
Hello, I know that macros are evil, but I recently came across a problem that I couldn't figure out how to solve with templates. I wanted to create a set of singleton event objects, and wrote...
8
by: boki_pfc | last post by:
Hi Everybody, I am looking for an advice on following: I have that "pleasure" of reading C++ codes that have been written by person(s) that have not attended the same C++ classes that I did or...
14
by: Alexander Stoyakin | last post by:
Hello! Please advise on the following issue. I have a function which returns 1 or 0 in multiple branches, and it must perform the same code before each return, like this: int fun() { if (some...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.