Understanding the Principal of Referenced Object & Referencing Object.

This tip submitted by Eddy Wu on 2013-01-08 11:27:49. It has been viewed 5426 times.
Rating of 5.3 with 33 votes



the core characteristic behaviour of reference vars are only to return the
instance object they refer to.

class A{
public:
A(int n = 2): mi(n) {}
~A() {}
const int& getvalue() const { return mi; }

friend const int* getInstanceMemberAddress(const A& instA);
private:
int mi;
int mj;
};


const int* getInstanceMemberAddress(const A& instA)
{
return &instA.mi;
}




using namespace std;

int main(int argc, char *argv[])
{
A a1;
A a2 = 5;

/* at this point 'a2.getvalue()' was interpreted as a const reference to int type it's job only to return the object it refers to.. which can be describes as followed:

{ const int& A:: mi; } */

int nn = a2.getvalue(); // int nn = const int& A::mi;












More tips

Help your fellow programmers! Add a tip!