473,507 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3078
> 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
5720
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
414
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
4960
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
1734
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
1629
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
3574
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
7111
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...
1
7031
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
7485
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...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.