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

Discussion in 'C++' started by amphetaman@gmail.com, Jul 4, 2008.

  1. Guest

    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, 2008
    #1
    1. Advertising

  2. Guest

    > Point &operator(const Point &);

    Oops, sorry, I missed a character there. It should be Point &operator
    =(const Point &).
    , Jul 4, 2008
    #2
    1. Advertising

  3. joseph cook Guest

    On Jul 3, 10:47 pm, 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
    joseph cook, Jul 4, 2008
    #3
  4. Guest

    On Jul 3, 10:58 pm, joseph cook <> wrote:
    > 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, 2008
    #4
  5. 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)
    Darío Griffo, Jul 4, 2008
    #5
  6. Jim Langston Guest

    <> wrote in message
    news:...
    > On Jul 3, 10:58 pm, joseph cook <> wrote:
    >> 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
    Jim Langston, Jul 4, 2008
    #6
  7. Linlin Yan Guest

    On Jul 4, 10:47 am, 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.
    Linlin Yan, Jul 4, 2008
    #7
  8. James Kanze Guest

    On Jul 4, 5:00 am, wrote:
    > On Jul 3, 10:58 pm, joseph cook <> wrote:


    > > 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:
    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
    James Kanze, Jul 4, 2008
    #8
  9. James Kanze Guest

    On Jul 4, 11:40 pm, "Alf P. Steinbach" <> wrote:
    > * 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:
    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
    James Kanze, Jul 5, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    20
    Views:
    7,880
    Roedy Green
    Sep 9, 2005
  2. Schnoffos
    Replies:
    2
    Views:
    1,188
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,604
    Old Wolf
    Jan 20, 2004
  4. venkatagmail
    Replies:
    11
    Views:
    657
    James Kanze
    Oct 3, 2007
  5. Tricky
    Replies:
    0
    Views:
    554
    Tricky
    Mar 19, 2009
Loading...

Share This Page