J
junw2000
Hi,
My textbook says that: "Once defined, a reference cannot be reassigned
because it is an alias to its target. What happens when you try to
reassign a reference turns out to be the assignment of a new value to
the target."
I try it using the following code:
#include <iostream>
using namespace std;
struct A {
virtual void f() { cout << "Class A" << endl; }
};
struct B: A {
void f(int) { cout << "Class B" << endl; }
};
struct C: B {
void f() { cout << "Class C" << endl; }
};
int main() {
int i = 10, i1 = 20;
int &ri = i;
ri = i1;
cout << "ri:" <<ri<<" i:"<<i<< endl; //LINE1
B b; C c;
A a;
A* pa1 = &b;
//b.f(); //LINE2
pa1->f(); //LINE3
A &aa = b;
aa.f();
cout << "###REFERENCE####" << endl;
aa = c;
aa.f(); //LINE4
// b.f() //LINE5
}
The output of LINE1 is---ri:20 i:20, so the textbook is correct.
The output of LINE4 is---Class A. And LINE5 can not compile (without
"//"), so b is not assigned the value c. This shows that the textbook
is wrong.
Is the textbook correct only for built-in type?
By the way, LINE2 can not compile due to B::f(int). The output of LINE3
is---Class A, so A::f() is not hiden by B::f(int). Is this due to
dynamic binding?
Thanks a lot.
Jack
My textbook says that: "Once defined, a reference cannot be reassigned
because it is an alias to its target. What happens when you try to
reassign a reference turns out to be the assignment of a new value to
the target."
I try it using the following code:
#include <iostream>
using namespace std;
struct A {
virtual void f() { cout << "Class A" << endl; }
};
struct B: A {
void f(int) { cout << "Class B" << endl; }
};
struct C: B {
void f() { cout << "Class C" << endl; }
};
int main() {
int i = 10, i1 = 20;
int &ri = i;
ri = i1;
cout << "ri:" <<ri<<" i:"<<i<< endl; //LINE1
B b; C c;
A a;
A* pa1 = &b;
//b.f(); //LINE2
pa1->f(); //LINE3
A &aa = b;
aa.f();
cout << "###REFERENCE####" << endl;
aa = c;
aa.f(); //LINE4
// b.f() //LINE5
}
The output of LINE1 is---ri:20 i:20, so the textbook is correct.
The output of LINE4 is---Class A. And LINE5 can not compile (without
"//"), so b is not assigned the value c. This shows that the textbook
is wrong.
Is the textbook correct only for built-in type?
By the way, LINE2 can not compile due to B::f(int). The output of LINE3
is---Class A, so A::f() is not hiden by B::f(int). Is this due to
dynamic binding?
Thanks a lot.
Jack