Returning const reference value from functions

Discussion in 'C++' started by Sree, Jun 24, 2004.

  1. Sree

    Sree Guest

    Hello All,
    When will it be benefical to return a value from a function as a const reference
    ?

    Say I have classes like this

    class B {
    public:
    int someValue1;
    int someValue2;
    void ChangeValue();
    void ConstFunc() const;
    };

    void B::ChangeValue()
    {
    int tmp = someValue1;
    someValue1 = someValue2;
    someValue2 = tmp;
    }


    class A {
    public :
    int value;
    void inspect(const B& b);
    const B& returnConstRef();
    private :
    int someInt;
    B classB;
    };

    const B& A::returnConstRef()
    {
    return classB;
    }

    In the above class, returnConstRef returns a const reference to B. So whatever
    we get like this should not be able to change class B like below. Also the
    compiler should not allow me to assign a non-const class B (classB) to classA.
    returnConstRef().

    A classA;
    B classB = classA.returnConstRef();
    classB.ChangeValue();

    With the above code there is no problem with the compiler. I am wrong some where
    in my understanding of the returning values by reference, Can somebody please
    correct ?

    Thanks in advance
    KInd.


    --
    Use our news server 'news.foorum.com' from anywhere.
    More details at: http://nnrpinfo.go.foorum.com/
    Sree, Jun 24, 2004
    #1
    1. Advertising

  2. Sree wrote:
    >
    > Hello All,
    > When will it be benefical to return a value from a function as a const reference
    > ?


    When the cost of returning by value is to high due to copying of the
    return value.
    With builtin types like int, double, char, ... this is practically never
    the case. User defined types need to have a closer inspection.

    [snip]

    > Also the
    > compiler should not allow me to assign a non-const class B (classB) to classA.
    > returnConstRef().


    Maybe I read something different to what you intended. But: Why should
    the compiler not allow this?

    >
    > A classA;
    > B classB = classA.returnConstRef();
    > classB.ChangeValue();


    That does something completely different.
    A new B object is constructed, which is initialized from the
    B object in A.
    After that classB and the B object in classA have nothing in common.
    This would not change, if you didn't return a const reference, but
    a non const instead.


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Jun 25, 2004
    #2
    1. Advertising

  3. In message <>, Sree
    <> writes
    >
    >Hello All,
    >When will it be benefical to return a value from a function as a const
    >reference
    >?


    See the thread "Read-only, as opposed to const member" for a lot more
    debate on this issue ;-)

    >
    >Say I have classes like this
    >
    >class B {
    >public:
    > int someValue1;
    > int someValue2;
    > void ChangeValue();
    > void ConstFunc() const;
    >};
    >
    >void B::ChangeValue()
    >{
    > int tmp = someValue1;
    > someValue1 = someValue2;
    > someValue2 = tmp;
    >}
    >
    >
    >class A {
    > public :
    > int value;
    > void inspect(const B& b);
    > const B& returnConstRef();
    > private :
    > int someInt;
    > B classB;
    >};
    >
    >const B& A::returnConstRef()
    >{
    > return classB;
    >}
    >
    >In the above class, returnConstRef returns a const reference to B. So whatever
    >we get like this should not be able to change class B like below. Also the
    >compiler should not allow me to assign a non-const class B (classB) to classA.
    >returnConstRef().
    >
    > A classA;
    > B classB = classA.returnConstRef();


    This constructs a new B which is a *copy* of the reference returned by
    returnConstRef(). The new B object has no connection with the one in
    classA.

    > classB.ChangeValue();


    And this can therefore modify the new copy.

    Did you perhaps mean to write

    B const & classB = classA.returnConstRef();
    // now classB is a (const) reference to classA.classB
    classB.ChangeValue(); // this won't compile
    ?
    >
    >With the above code there is no problem with the compiler. I am wrong
    >some where
    >in my understanding of the returning values by reference, Can somebody please
    >correct ?
    >


    --
    Richard Herring
    Richard Herring, Jun 25, 2004
    #3
  4. Sree

    JKop Guest

    Sree posted:

    > In the above class, returnConstRef returns a const reference to B. So
    > whatever we get like this should not be able to change class B like
    > below.


    Wouldn't it be nice if you could make the B object read-only.

    class A
    {
    read-only B objectB;
    };


    I've written another thread about this entitled "Read only, as opposed to
    const member variable". Don't look at my original post, but my second one in
    the thread. Essentially it does this:

    int main(void)
    {
    A a;

    TakesB(a.objectB);

    a.objectB = 4; //Compile ERROR

    //Essentially, it is read-only
    }


    -JKop
    JKop, Jun 25, 2004
    #4
    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. Replies:
    11
    Views:
    1,101
  2. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  3. George2
    Replies:
    10
    Views:
    595
    Pete Becker
    Dec 17, 2007
  4. 0m
    Replies:
    26
    Views:
    1,114
    Tim Rentsch
    Nov 10, 2008
  5. Jens Thoms Toerring

    Const/non-const pointer returning method

    Jens Thoms Toerring, May 25, 2010, in forum: C++
    Replies:
    11
    Views:
    720
    John H.
    May 26, 2010
Loading...

Share This Page