Re: [Dev-C++] Passing by value or reference to constructor initialiation list
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Esteban V. <est...@ho...> - 2003-01-10 13:47:21
|
To pass objects by reference is the better way. You don´t know how many data
members were declarated in the class declaration. So if you pass by value
you force stack to copy each of the data members of the class. Moreover, I
recommend you to pass objects by reference with the const key to give the
less stack overload and the better protection of the public data member of
the class. This is
void MyFunction ( const OtherClass & className)
{
....
}
When you pass or recive by value an object, the copy constructor is
automatically called to give and receive data members to and from the stack
with the time overload of construct and destroy temporary objects. Whe you
pass by reference nothing of this happens and you only put in the stack the
object memory direction. To learn more about this I recommend you to see
operator overloading in any C++ text book. In this chapter you will see both
pass by reference and receive by reference and value objects.
With respect to integers, depends of you.
Best regards, Esteban
----- Original Message -----
From: "Daniel Glenfield" <sp...@ho...>
To: <dev...@li...>
Sent: Friday, 10 January, 2003 6:19
Subject: [Dev-C++] Passing by value or reference to constructor
initialiation list
I would like to know whether it is better to pass the following string
objects and integer by value or reference to my constructor initialisation
list.
Vehicle::Vehicle
(
String& VehicleName,
String& VehicleType,
int SerialNumber
):
mVehicleName(VehicleName),
mVehicleType(VehicleType),
mSerialNumber(SerialNumber)
{
}
an integer is only 4 bytes I think, so by value would be fine, but what
about the string objects? Can I pass by reference?
_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|