473,651 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reference/value semantics in constructors.

This is something that has been bothering me for a while.

Assume the following two class definitions:

class Inner {
int a;
int b;
...
};

class Outer {
Inner a;
int aa;
int bb;
};

Further assume that Outer's constructor takes an Inner object as an
argument. Keeping in mind that Outer contains its own Inner object
and not a reference to a 'foreign' Inner, should Outer's constructor
be:

Outer(Inner a) : a(a) {...}

OR

Outer(Inner& a) : a(a) {...}

?

I know they both have the same outcome, but I'm asking this question
from a performnce standpoint. Assume Inner is larger than four bytes.

Thanks.
Jul 22 '05 #1
2 1229

"Squeamizh" <ad**@adf.ads f> wrote in message
news:gv******** *************** *********@4ax.c om...
This is something that has been bothering me for a while.

Assume the following two class definitions:

class Inner {
int a;
int b;
...
};

class Outer {
Inner a;
int aa;
int bb;
};

Further assume that Outer's constructor takes an Inner object as an
argument. Keeping in mind that Outer contains its own Inner object
and not a reference to a 'foreign' Inner, should Outer's constructor
be:

Outer(Inner a) : a(a) {...}

OR

Outer(Inner& a) : a(a) {...}

?

I know they both have the same outcome, but I'm asking this question
from a performnce standpoint. Assume Inner is larger than four bytes.


If the type of the argument will be 'large', passing by reference is
usually preferred. If the argument must/will not be modified, pass
by const reference.

The actual 'physical' speed and sizes of data types will depend upon
your implementation and platform, so the only way to find out for
sure which is 'faster' would be to test them.

-Mike
Jul 22 '05 #2
On 2004-09-10, Squeamizh <ad**@adf.ads f> wrote:
Outer(Inner a) : a(a) {...}

OR

Outer(Inner& a) : a(a) {...}


In your particular case, I would suggest

Outer(const Inner& new_a) : a(new_a) {...}

Passing parameters by value usually make sense only when copying is
inexpensive (e.g. for int), or when you have some specific logic in the
object's copy constructor (e.g. std::auto_ptr, boost::shared_p tr)

Hope this helps!

--
Sergei Matusevich,
Brainbench MVP for C++
http://www.brainbench.com

Jul 22 '05 #3

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

Similar topics

36
28094
by: Riccardo Rossi | last post by:
Hi all! How does Python pass arguments to a function? By value or by reference? Thanks, Riccardo Rossi.
10
2262
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in the class template so I know how many references I have to the body. In my case it's the Integer wrapper class which is the body. This template works for many different types. The one that I use is named Integer and is a Wrapper for an int. The...
19
2138
by: daniel | last post by:
This is a pretty basic-level question, but I'd really like to know, so thanks for any help or pointers you can provide (like what I would google for ;o) Suppose: <code> myFunc() {
18
3523
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
23
1955
by: sidney | last post by:
Hi all, I am trying to make a vector containing objects the have a reference member. However, as soon as I try to push_back an element into this vector, g++ balks at the fact that it needs to instantiate an operator= on the class containing the reference members (see below for what I try to do). #include <vector>
10
13644
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the function is also modified. Sometimes I wish to work with "copies", in that when I pass in an integer variable into a function, I want the function to be modifying a COPY, not the reference. Is this possible?
4
1418
by: Wayne Shu | last post by:
Hi everyone: I am reading Bjarne Stroustrup's The C++ Programming Language(Special Edition). In section 7.3, bs wrote "Like the semantics of argument passing, the semantics of function value return are identical to the semantics of initialization. A return statement is considered to initialize an unnamed variable of the returned type."
41
3639
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what pointer and reference are and how they relate to each other.
275
12244
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
8792
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8694
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8457
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8571
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7294
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5605
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2696
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 we have to send another system
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.