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

A

amphetaman

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?)
 
A

amphetaman

Point &operator(const Point &);

Oops, sorry, I missed a character there. It should be Point &operator
=(const Point &).
 
J

joseph cook

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
 
A

amphetaman

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"?
 
D

Darío Griffo

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)
 
J

Jim Langston

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.
 
L

Linlin Yan

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.
 
J

James Kanze

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.
 
J

James Kanze

* Paavo Helde:

[...]
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,166
Latest member
DollyBff32
Top