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

multiple constructors and class reference variables

Hello,

AFAIK the only way to initialize a reference variable defined inside a class
is to initialize it in an initializer list. However, when there are multiple
constructors, this means that the initializer lists have to be cut and pasted
from one constructor to another. This does not seem to lend itself particularly
well to maintainablility. Calling a constructor from another in C++ is not legal
unlike in Java. Also, functions other than constructors cannot have initializer
lists. So, if there are multiple constructors, it seems to me that the references
need be copied and constructed the same in each initializer list. This seems
somewhat tedious. Of course, one could always use the preprocessor and
declare the list of common reference initializers in a macro and
then paste the macro instead. I don't regard this particuar
style very good either.

Comments welcome,

Regards,

Neil
Jul 22 '05 #1
2 3062
> AFAIK the only way to initialize a reference variable defined inside a
class
is to initialize it in an initializer list. However, when there are multiple constructors, this means that the initializer lists have to be cut and pasted from one constructor to another. This does not seem to lend itself particularly well to maintainablility. Calling a constructor from another in C++ is not legal unlike in Java. Also, functions other than constructors cannot have initializer lists. So, if there are multiple constructors, it seems to me that the references need be copied and constructed the same in each initializer list. This seems somewhat tedious. Of course, one could always use the preprocessor and
declare the list of common reference initializers in a macro and
then paste the macro instead. I don't regard this particuar
style very good either.


You might create an additional constructor as a constructor in a second
class:

class a {
int & n;
public:
a(int & i):
n(i)
{
...
}
};

class b: public a {
public:
b(int & i; int j):
a(i)
{
...
}
};

This would actually work as if the constructors can call each other (which
they do).

Niels Dybdahl

Jul 22 '05 #2

"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
Hello,

AFAIK the only way to initialize a reference variable defined inside a class is to initialize it in an initializer list. However, when there are multiple constructors, this means that the initializer lists have to be cut and pasted from one constructor to another. This does not seem to lend itself particularly well to maintainablility. Calling a constructor from another in C++ is not legal unlike in Java. Also, functions other than constructors cannot have initializer lists. So, if there are multiple constructors, it seems to me that the references need be copied and constructed the same in each initializer list. This seems somewhat tedious. Of course, one could always use the preprocessor and
declare the list of common reference initializers in a macro and
then paste the macro instead. I don't regard this particuar
style very good either.

Comments welcome,

Regards,

Neil


Don't put references in classes - use pointers.
- people reading the impl wont realize it is a ref
- you can't copy references so you can't implement copy ctor or assignment
properly
Jul 22 '05 #3

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
2
by: Neil Zanella | last post by:
Hello, AFAIK the only way to initialize a reference variable defined inside a class is to initialize it in an initializer list. However, when there are multiple constructors, this means that the...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
10
by: John | last post by:
Trying to find out what is essential / optional, I made an extremely simple Class and Module combination to add two numbers. (see below) It appears that an empty constructor is needed n order to...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
14
by: dl | last post by:
I have two classes, say A and B, both having a data member 'int n'; private in A, public in B. When I derive class C from both public A and public B, B::n should be visible to C while A::n...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.