My concern is about run-time polymorphism. I quite understand the notion of using base-class pointers and the "virtual" keyword in order to achieve it. And I do understand why the compiler defers call resolution to run-time. But, I am confused thinking why the compiler would defer it when references are used, instead of pointers. A reference, once assigned, cannot refer to anything else. So, doesn't the compiler already know what object the reference refers to? So, why doesn't it statically resolve function calls when there is a base-class reference, not pointer? This is different for a pointer because it can point to any object at run-time within the class hierarchy. But the reference is fixed!
Here is a code snippet:
class Base
{
protected:
int m_value;
public:
Base(int value)
: m_value(value)
{
}
virtual const char* getName() const { return "Base"; }
int getValue() const { return m_value; }
};
class Derived: public Base
{
public:
Derived(int value)
: Base(value)
{
}
virtual const char* getName() const { return "Derived"; }
};
int main()
{
Derived derived(5);
std::cout << "derived is a " << derived.getName() << " and has value " <<
derived.getValue() << '\n';
Base &ref = derived;
std::cout << "ref is a " << ref.getName() << " and has value " << ref.getValue() << '\n';
Base *ptr = &derived;
std::cout << "ptr is a " << ptr->getName() << " and has value " << ptr- >getValue() << '\n';
return 0;
}
Aucun commentaire:
Enregistrer un commentaire