Can a reference refer to more than one object during its lifetime?
<p>As I know a reference can refer to only one object during its lifetime..
However,the code bellow compiles correctly.. although I have changed the referred object.. The output is:1.
How can it compile correctly?</p>

<p>Thanks
Shiran</p>

<pre><code>class A{
 private:
int a;
 public:
A(int a):a(a){}
virtual ~A(){}
virtual void f()const {cout&lt;&lt;a&lt;&lt;endl;}
};
class B: public A{
 private:
int b;
 public:
B(int Ina,int Inb):A(Ina),b(Inb){}
virtual void f()const {cout&lt;&lt;b&lt;&lt;endl;}
};

int main(){
    B b(1,2);
    A a(5);
    A&amp; ref=a;
    ref=b;
    ref.f();
    return 0;
}
</code></pre>
