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

Do I pass an int by value or reference in constructor?

I have a very simple class for storing two-dimensional coordinates:

class Point
{
public:
Point();
Point(const Point &);
Point &operator(const Point &);
int x;
int y;
};

If I want a constructor that takes two ints, is it better to pass by
value or by reference? In other words, Point(int, int) or Point(const
int &, const int &)? (Or maybe Point(const int, const int)? Does that
actually improve performance?)
Jul 4 '08 #1
10 2378
Point &operator(const Point &);

Oops, sorry, I missed a character there. It should be Point &operator
=(const Point &).
Jul 4 '08 #2
On Jul 3, 10:47*pm, ampheta...@gmail.com wrote:
I have a very simple class for storing two-dimensional coordinates:

class Point
{
public:
* * Point();
* * Point(const Point &);
* * Point &operator(const Point &);
* * int x;
* * int y;

};

If I want a constructor that takes two ints, is it better to pass by
value or by reference? In other words, Point(int, int) or Point(const
int &, const int &)? (Or maybe Point(const int, const int)? Does that
actually improve performance?)
For built-in types, you shouldn't see any performance difference
passing by reference or value. In fact, for a class as simple as the
one you sent, the function call itself should be optimized away by the
compiler entirely.

Joe Cook
Jul 4 '08 #3
On Jul 3, 10:58 pm, joseph cook <joec...@gmail.comwrote:
For built-in types, you shouldn't see any performance difference
passing by reference or value. In fact, for a class as simple as the
one you sent, the function call itself should be optimized away by the
compiler entirely.

Joe Cook
Thanks. But still, are there any guidelines on whether to pass by
value or by reference? Is one or the other a "better style"?
Jul 4 '08 #4
ampheta...@gmail.com wrote:
If I want a constructor that takes two ints, is it better to pass by
value or by reference? In other words, Point(int, int) or Point(const
int &, const int &)? (Or maybe Point(const int, const int)? Does that
actually improve performance?)
Answer this 2 questions:

if passing a reference... what object/native type is pushed into the
stack?
if passing an int b value what object/native type is pushed into the
stack?

To answer the first, you should answer before yourself
What is a reference? (implementation point of view)
Jul 4 '08 #5
<am********@gmail.comwrote in message
news:b8**********************************@u6g2000p rc.googlegroups.com...
On Jul 3, 10:58 pm, joseph cook <joec...@gmail.comwrote:
>For built-in types, you shouldn't see any performance difference
passing by reference or value. In fact, for a class as simple as the
one you sent, the function call itself should be optimized away by the
compiler entirely.

Joe Cook

Thanks. But still, are there any guidelines on whether to pass by
value or by reference? Is one or the other a "better style"?
I would say in general it is probably better to pass by const referance when
you can. In general meaing for structures, classes, etc.. since this would
be faster than passing by value. For built in types it is my understanding
that references may actually be a little slower (but not by much) and may
get optimized away anyway.

My general rules for paramters are:
Make const whever I can
Make it a reference whenever I can and it's not built in
Don't make it a reference if it obfuscates the code.

Your mileage may vary.

--
Jim Langston
Jul 4 '08 #6
On Jul 4, 10:47*am, ampheta...@gmail.com wrote:
I have a very simple class for storing two-dimensional coordinates:

class Point
{
public:
* * Point();
* * Point(const Point &);
* * Point &operator(const Point &);
* * int x;
* * int y;

};

If I want a constructor that takes two ints, is it better to pass by
value or by reference? In other words, Point(int, int) or Point(const
int &, const int &)? (Or maybe Point(const int, const int)? Does that
actually improve performance?)
I prefer Point(int x, int y). Because it may do dereferencing if you
use Point(const int& x, const int& y). But as joseph said, compiler
could do optimizing for you. And Point(const int x, const int y) is
recommended if you want to express the constance of the parameters.
Jul 4 '08 #7
On Jul 4, 5:00 am, ampheta...@gmail.com wrote:
On Jul 3, 10:58 pm, joseph cook <joec...@gmail.comwrote:
For built-in types, you shouldn't see any performance
difference passing by reference or value. In fact, for a
class as simple as the one you sent, the function call
itself should be optimized away by the compiler entirely.

Thanks. But still, are there any guidelines on whether to pass
by value or by reference? Is one or the other a "better
style"?
It depends on who wrote the style guidelines:-). The most
frequent recommendation seems to be to use pass by value for
scalar types, pass by const reference for class types (and pass
by const reference in templates, when the actual type isn't
known). About the only other recommendation I've heard is to
consider pass by const reference an "optimziation", to be
applied only when the profiler says it's necessary.

Of course, a lot of people don't bother with guidelines, and do
things on an ad hoc basis, according to their feeling of the
moment. Thus, the standard library tends to pass iterators and
all types of functional objects by value, even though they're
not scalar types.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 4 '08 #8
am********@gmail.com kirjutas:
I have a very simple class for storing two-dimensional coordinates:

class Point
{
public:
Point();
Point(const Point &);
Point &operator(const Point &);
int x;
int y;
};

If I want a constructor that takes two ints, is it better to pass by
value or by reference? In other words, Point(int, int) or Point(const
int &, const int &)? (Or maybe Point(const int, const int)? Does that
actually improve performance?)
It does not make any difference, unless your class is used in a tight
loop in a performance-critical application (and then you have to profile
anyway to find out the bottlenecks).

That said, on current hardware a reference typically takes 64 bit plus a
dereference step and an int typically takes 32 bit and no dereference. So
I would go with pass by value in this case, as it is most probably not
slower than the alternative. If the functions are inline, the slower
version is probably optimized to the other anyway, so it's actually not
so important point to worry about.

hth
Paavo

PS. The 'const int' version you mentioned is the same as 'int',
performance-wise. The const keyword in general cannot be used for
optimization because it could be cast away legally in the inners of the
function.

Jul 4 '08 #9
"Alf P. Steinbach" <al***@start.nokirjutas:
Not all the world is 64-bit Windows.
I was primarily thinking of 64-bit Linux, on a 16-core Intel CPU. It's one
of our main development targets currently.

YMMV of course ;-)

Best regards
Paavo


Jul 4 '08 #10
On Jul 4, 11:40 pm, "Alf P. Steinbach" <al...@start.nowrote:
* Paavo Helde:
[...]
That said, on current hardware a reference typically takes
64 bit plus a dereference step and an int typically takes 32
bit and no dereference.
Not all the world is 64-bit Windows.
Nope. Most of us have been 64 bits for close to 10 years
now:-). (Except maybe for mainframes, where 36 and 48 bits can
also be found. Or for those working on embedded processors,
where I think 16 bits is still common. Or who knows what else.)

And yes, I'm just trying to be humorous. Your point is well
taken (although you can drop the Windows---my 64 bit machines
run under Solaris or Linux).

(Of course, all considerations with regards to what the compiler
might generate, supposing any specific architecture, are really
irrelevant anyway. Since compilers don't always do what we
expect. Any rule is arbitrary, so just pick one, and stick to
it until actual measurements say you need to change.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 5 '08 #11

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

Similar topics

5
by: Jan Pieter Kunst | last post by:
(apologies if this message is a duplicate -- my news server seems to have problems) Greetings, When using PHP 4, this: // ex. 1 class A { function A(&$obj) {
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
4
by: Omar Llanos | last post by:
Recently, I posted a question on how to invoke a textbox control in Form1 (parent form) from within Form2 (which is inherited from Form1). Someone suggested to pass a reference of the Form1 to the...
7
by: SB | last post by:
What is the proper way to pass a character array (char *) from a "C" dll to a C# method (delegate) in my app? Getting the dll (which simulates a third party dll) to call my delegate works fine. ...
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
5
by: Dariusz Bismor | last post by:
Hi, Please help me to understand compiler behavior concerning the following code: class X{    int k; public:    X(){ k=1; }    X( X & model ){       k = model.k;
7
by: Jess | last post by:
Hello, I learned that when I work with templates in C++, I should have functions that pass arguments by reference because the type of object is not known. Does it mean that if I have a function...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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
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...
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...
0
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...

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.