C++ makes possible to reassign reference?

Viewed 2963
#include <iostream>
using namespace std;

class Base{
    public:
    virtual void f(){
        std::cout << "Base\n";
    }
};

class Derived1 : public Base{
    public:
    void f(){
        std::cout << "Derived1\n";
    }

};

class Derived2 : public Base{
    public:
    void f(){
        std::cout << "Derived2\n";
    }
};




int main() {
    Derived1 d1;
    Derived2 d2;

    Base& ref = d1;
    ref.f();
    ref = d2;
    ref.f();

    return 0;
}

It is said that the reference cannot be reassigned. Here, the compiler accept this code and output is not understandable for me. Output:

Derived1 Derived1

So, what does mean ref = d2; in fact?

2 Answers
Related