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

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 1221

"Squeamizh" <ad**@adf.adsf> wrote in message
news:gv********************************@4ax.com...
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.adsf> 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_ptr)

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
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
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...
19
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
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
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...
10
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...
4
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...
41
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...
275
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
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...

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.