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?
CRectangle rec
, and I callrec.getName()
, for his first version (return by reference), does the function call return a reference to rec? – Cardoso