constant references?

Discussion in 'C++' started by Rickard Andersson, Nov 25, 2003.

  1. What it the use of constant references in methods and functions? Is it a
    design issue or hwat?

    A reference that cannot be edited? huh?
    Why pass a reference then?
     
    Rickard Andersson, Nov 25, 2003
    #1
    1. Advertising

  2. Rickard Andersson wrote:
    > What it the use of constant references in methods and functions? Is it a
    > design issue or hwat?
    >
    > A reference that cannot be edited? huh?
    > Why pass a reference then?
    >


    At least one example of when constant references are useful is the case
    when you want to pass an object as a read only parameter to some function:


    class A
    {
    // add members here
    }

    void foo(const A& a)
    {
    // access a thru read only operations
    }

    By declaring the parameter 'a' as a const refernce you tell users of
    foo() that their object won't be altered as a result of the call to
    foo(). You also enable clever compilers to make assumptions on how 'a'
    will be used inside foo() and based on that they might be able to do
    some optimisations when compiling your code. Last but not least, if the
    size of A should large you will avoid passing the whole object and
    instead pass a suposedly much smaller reference.

    :.:: mattias
     
    =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=, Nov 25, 2003
    #2
    1. Advertising

  3. Okay, how do I know when I should pass a const reference and not a simple
    const?

    Dummy question, but it's confusing to me!

    btw, you're swedish living in Luleå? :)


    "Mattias Brändström" <> skrev i meddelandet
    news:3fc35178$0$164$...
    > Rickard Andersson wrote:
    > > What it the use of constant references in methods and functions? Is it a
    > > design issue or hwat?
    > >
    > > A reference that cannot be edited? huh?
    > > Why pass a reference then?
    > >

    >
    > At least one example of when constant references are useful is the case
    > when you want to pass an object as a read only parameter to some function:
    >
    >
    > class A
    > {
    > // add members here
    > }
    >
    > void foo(const A& a)
    > {
    > // access a thru read only operations
    > }
    >
    > By declaring the parameter 'a' as a const refernce you tell users of
    > foo() that their object won't be altered as a result of the call to
    > foo(). You also enable clever compilers to make assumptions on how 'a'
    > will be used inside foo() and based on that they might be able to do
    > some optimisations when compiling your code. Last but not least, if the
    > size of A should large you will avoid passing the whole object and
    > instead pass a suposedly much smaller reference.
    >
    > :.:: mattias
    >
     
    Rickard Andersson, Nov 25, 2003
    #3
  4. Rickard Andersson wrote:
    > Okay, how do I know when I should pass a const reference and not a simple
    > const?
    >


    I guess you mean when you should one of the following

    (1) void foo(const A& a);

    (2) void foo(const A a);

    My short answer to this would be: it makes little sense to do something
    like (2). Since you are passing your A object by value the variable you
    pass to foo() will be copied and there will be now way for foo() to
    alter it. Declaring 'foo(const A a)' doesn't tell users of foo()
    anything more than if you would declare it 'foo(A a)'.

    :.:: mattias

    PS.
    > btw, you're swedish living in Luleå?

    I'm swedish and living in Gothenburg. I used to study in Luleå.
     
    =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=, Nov 25, 2003
    #4
  5. Rickard Andersson

    tom_usenet Guest

    On Tue, 25 Nov 2003 12:42:43 GMT, "Rickard Andersson"
    <> wrote:

    >What it the use of constant references in methods and functions? Is it a
    >design issue or hwat?
    >
    >A reference that cannot be edited? huh?
    >Why pass a reference then?


    If you pass by value, a copy of the thing you are passing will be
    made. Some objects either aren't copyable or are inefficient to copy,
    so you pass them by const reference (or pointer). Finally, if you want
    polymorphism, you have to use pass by reference otherwise you get
    slicing. e.g.

    void foo(const Base& b);

    foo(aDerived);

    Tom
    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Nov 25, 2003
    #5
  6. Rickard Andersson

    Rolf Magnus Guest

    Rickard Andersson wrote:

    > Okay, how do I know when I should pass a const reference and not a
    > simple const?


    Well, a "simple const" as parameter isn't very useful. A function taking
    an argument by value cannot modify that argument anyway, since the
    function receives a copy. With some class types, a copy can easily get
    quite expensive, and that's when you use call-by-reference instead of
    call-by-value. And if the function doesn't modify the object, you use a
    const reference. I think many people use call-by-value for builtin
    small types and call-by-(const-)reference for most class types.
     
    Rolf Magnus, Nov 25, 2003
    #6
  7. Rickard Andersson

    jeffc Guest

    "Rickard Andersson" <> wrote in message
    news:77Iwb.39189$...
    > What it the use of constant references in methods and functions? Is it a
    > design issue or hwat?
    >
    > A reference that cannot be edited? huh?
    > Why pass a reference then?


    What is the alternative? Pass by value. When you pass by value, you make a
    copy. If the object is tiny, it's no big deal. But some objects are not
    tiny.
     
    jeffc, Nov 25, 2003
    #7
  8. Mattias Brändström wrote:

    > Rickard Andersson wrote:
    >
    >> Okay, how do I know when I should [implement a function]
    >> to pass a const reference and not a simple const?

    >
    > I guess you mean when you should one of the following
    >
    > (1) void foo(const A& a);
    >
    > (2) void foo(const A a);


    The second declaration is equivalent to

    void foo(A a);

    Identifier a is the *formal* argument.
    When you invoke foo:

    A a;
    foo(a);

    identifier a is the *actual* argument.
    The compiler emits code to pass a by reference in the first case and
    the compiler emits code to pass a by value in the second case.
    It does *not* matter whether a is passed by value or const reference
    as far as the program that invokes foo is concerned.
    The only difference is that pass by reference doesn't require
    the copy of the actual argument like pass by value.
    This is an important optimization
    only when a is larger than about one or two machine words.

    > My short answer to this would be:
    > it makes little sense to do something like (2).
    > Since you are passing your A object by value,
    > the [actual argument] you pass to foo() will be copied


    [into the formal argument]

    > and there will be now way for foo() to alter [the actual argument].
    > Declaring 'foo(const A a)' doesn't tell users of foo()
    > anything more than if you would declare it 'foo(A a)'.


    Using the const qualifier for formal arguments passed by value
    is only useful in the function *definition*:

    void foo(const A a) {
    // The const qualifier informs the compiler that
    // any attempt to modify a in the body of the function
    // is a mistake and the compiler should issue a warning.
    }
     
    E. Robert Tisdale, Nov 25, 2003
    #8
  9. Rickard Andersson

    jeffc Guest

    "E. Robert Tisdale" <> wrote in message
    news:...
    > >
    > > (1) void foo(const A& a);
    > >
    > > (2) void foo(const A a);

    >
    > The second declaration is equivalent to
    >
    > void foo(A a);


    Not really. If the definition is

    void foo(const A a)
    {
    a = 3;
    }

    then there will be a compile error for (2).
     
    jeffc, Nov 25, 2003
    #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. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,094
  2. Martin Magnusson
    Replies:
    2
    Views:
    508
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    564
    Tim Roberts
    Nov 23, 2006
  4. Replies:
    4
    Views:
    339
    Keith Thompson
    Dec 14, 2006
  5. Replies:
    13
    Views:
    12,934
    Kai-Uwe Bux
    Jan 22, 2007
Loading...

Share This Page