"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:42***************@gascad.at
What you see as 'reseating' a reference has a very different meaning
to the compiler:
A a;
B b;
Abstract& tmp=a;
tmp=b;
You read this as: reseat the reference.
The compiler reads this as: take the value of b and assign it to tmp.
Now,
tmp is an alias for a, so the whole thing is equivalent to
a = b;
I am sure you know this, but for the benefit of the OP:
The fact that the reference is to the base class means that what is actually
being assigned is the base component of b to the base component of a. To
make the point clearer, let us consider a Base and Derived class, each of
which has a data member. We consider 4 different objects of the Derived
class.
If you run the following code, you will see that assignment via the Base
reference changes the data member inherited from the Base class, while not
changing the data member from the Derived class. Assignment via a Derived
reference, by contrast, changes the data member from both the Base and the
Derived class, as does direct assignment.
#include <iostream>
using namespace std;
class Base
{
int baseMember;
protected:
Base(int arg) : baseMember(arg)
{}
public:
void PrintMember()
{
cout << "baseMember is " << baseMember << ". ";
};
};
class Derived: public Base
{
int derivedMember;
public:
Derived(int arg) : Base(arg), derivedMember(arg)
{}
void PrintMember()
{
Base::PrintMember();
cout << "derivedMember is " << derivedMember << endl;
};
};
int main()
{
Derived d1(1), d2(2), d3(3), d4(4);
cout << "After construction:\n";
cout << "d1: ";
d1.PrintMember();
cout << "d2: ";
d2.PrintMember();
cout << "d3: ";
d3.PrintMember();
cout << "d4: ";
d4.PrintMember();
Base& d1_base_alias = d1;
d1_base_alias = d2;
cout << "\nAfter assignment of d2 to d1 via Base reference:\n";
cout << "d1: ";
d1.PrintMember();
Derived& d1_derived_alias = d1;
d1_derived_alias = d3;
cout << "\nAfter assignment of d3 to d1 via Derived reference:\n";
cout << "d1: ";
d1.PrintMember();
d1 = d4;
cout << "\nAfter direct assignment of d4 to d1:\n";
cout << "d1: ";
d1.PrintMember();
return 0;
}
--
John Carson