Whats the difference between Reference and Pointer return types in C++
Asked Answered
C

4

15

If I were to create a simple object in C++, what is the difference between returning an address of the member vs. returning a pointer. As far as I'm aware, C++ doesn't have automatic garbage collection so it wouldn't be keeping a reference count. So why would someone do it this way:

class CRectangle {
public:
    string& getName( );
    int&    getWidth( );
    int&    getHeight( );
private:
    string  name;
    int     height;
    int     width;
};

rather than this way:

class CRectangle {
public:
    string* getName( );
    int*    getWidth( );
    int*    getHeight( );
private:
    string  name;
    int     height;
    int     width;
};

I realize these would allow you to access member data, but I'm not concerned about proper encapsulation in this simple example. So whats the difference? Speedup? Readability? Style?

Chromium answered 4/4, 2011 at 17:14 Comment(0)
V
23

The & (in this context) doesn't mean "address of".

A function declared as string& getName( ); returns a reference, not a pointer. A reference is essentially an alias for another object. So in this case, it doesn't return a copy of the object's name, or a pointer to the name, but a reference to the name itself. So any changes to the returned object are directly applied to the object's name.

You could achieve the same by returning a pointer, but there'd be two significant differences:

  • a pointer requires special syntax to access (the * and -> operators), whereas a reference is used the exact same way you'd use the object itself.
  • a pointer can be null, a reference cannot. So any time a pointer is used, you are signalling to the reader of the code that "this value may be null"
Voracity answered 4/4, 2011 at 17:18 Comment(2)
Hi, just wondering, if I declare CRectangle rec, and I call rec.getName(), for his first version (return by reference), does the function call return a reference to rec?Cardoso
I am just curious what object the string & refers to?Cardoso
O
10

In this context, the & means reference - not address-of.

  • A pointer can be 0 - A reference cannot, in a well-formed program.
  • Ownership may be more unclear when you return a pointer.
  • Readability is subjective - I'd say use a reference when you can.
  • For efficiency, there's no real difference.
Outspeak answered 4/4, 2011 at 17:17 Comment(0)
A
0

First off, the & form is called a "reference" in C++. The * form is called "pointer" or "address of".

Speedup - no difference. Readability - probably a slight advantage for references, no derefencing in the upstack code.

Style - explicit breaking of encapsulation is bad enough style to obbscure anything else. If you want an object with naked data, an all-public struct would do just as well.

Allodium answered 4/4, 2011 at 17:20 Comment(0)
S
0

There's no benefit I can think of in returning a pointer to an integer from a property-type method such as getWidth(). It adds the burden of having to track memory ownership; but is actually likely to increase the bandwidth of the return type (both int and pointer sizes are platform specific, and can and do vary, but typical sizes are 32 and 64 bits respectively).

Shimmery answered 4/4, 2011 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.