F
Fernando Gómez
Hello all. I have this class with a virtual method and a constructor
that calls this virtual method. A derived class overrides this virtual
method, so I expected that when the base's constructor is called, it
would call the derived version of the method. However, it does not, it
calls the base's version. An example:
class Base
{
public:
Base() {
Method();
}
virtual ~Base() { }
virtual void Method(const Base& base) {
cout << "Base::Method" << endl;
}
};
class Derived : public Base
{
public:
Derived() : Base() { }
virtual void Method() {
cout << "Derived::Method" << endl;
}
};
int main()
{
Derived d; // prints "Base::Method" !!!
d.Method; // prints "Derived::Method"
return EXIT_SUCCESS;
}
Am I missing something here? Is calling virtual members not allowed on
the constructors? Would this be a bug from my compiler?
Thanks in advance.
that calls this virtual method. A derived class overrides this virtual
method, so I expected that when the base's constructor is called, it
would call the derived version of the method. However, it does not, it
calls the base's version. An example:
class Base
{
public:
Base() {
Method();
}
virtual ~Base() { }
virtual void Method(const Base& base) {
cout << "Base::Method" << endl;
}
};
class Derived : public Base
{
public:
Derived() : Base() { }
virtual void Method() {
cout << "Derived::Method" << endl;
}
};
int main()
{
Derived d; // prints "Base::Method" !!!
d.Method; // prints "Derived::Method"
return EXIT_SUCCESS;
}
Am I missing something here? Is calling virtual members not allowed on
the constructors? Would this be a bug from my compiler?
Thanks in advance.