Here's the code:
class Base {
public:
virtual void f() {
std::cout << "Base::f()" << std::endl;
std::cout << std::endl;
};
};
class Derived : public Base {
public:
void f() override {
std::cout << "Derived::f()" << std::endl;
std::cout << std::endl;
};
};
int main(int argc, char** argv) {
Base* d = new Derived();
Base without_ref = *d;
Base& with_ref = *d;
without_ref.f();
with_ref.f();
}
Here's the result:
Base::f()
Derived::f()
Could anybody explains this? In my opinion, they should have the same behavior.