Your question asks :
Quote:
Can we initialize a reference variable with a pointer variable?
Yes we can: your code demonstrates that.
The next thing is "
Quote:
both should have given the same value. Why they are different?
The output is correct. This is actually why we use pointers.
Pointers - C++ Tutorials[
^]
int main()
{
Base b1(5);
Base *b3 = &b1;
Base &b2 = *b3;
b2.geta();
b3->geta();
Base b4(10);
b3 = &b4;
cout << "a=" << b3->a << endl;
b2.geta();
b3->geta();
return 0;
}
You set the pointer b3 to point to b1 but later you set it to point to b4. Thus the output is correct.
You can view what happens using your debubugger.