0

Code 1

class Drink
        {
        public:
        Drink( char *name_ ) : name( name_ ) {}
        char *name;
        char  get(int i) const { return name[i]; }
       };

Code 2

class Drink
        {
        public:
        Drink( char *name_ ) : name( name_ ) {}
        char *name;
        char & get(int i) const { return name[i]; }
       };

Why the second code is better

2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by Ancient Dragon
0

The only reason I know for returning the reference is when overloading operator[] so that it can be changed by the calling function. I suppose if the object being returned is large enough one could return a reference (or pointer) instead of a copy just to save processing time. In your example, the first version is the best.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.