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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.