Tuesday, 15 July 2014

c++ polymorphism, name resolution for derived class -



c++ polymorphism, name resolution for derived class -

class base of operations { public: virtual void f(float) { cout << "base::f(float)\n"; } }; class derived : public base of operations { public: virtual void f(int) { cout << "derived::f(int)\n"; } }; int main() { derived *d = new derived(); base of operations *b = d; b->f(3.14f); d->f(3.14f); }

-c++ not back upwards contravaraint homecoming type, f(int) not override f(float)

-polymorphism supported c++, both d , b should point vtable derived class.

-vtable derived class 0: f(float), 1: f(int) etc.

my reply question base::f(float) called twice, reply is:

base::f(float) derived::f(int)

why case? accessing derived class different pointer enforce rules? far aware, object slicing occurs when using copy-ctor or copy-assignment, pointers, should point same vtable.

methods in different classes not overload together; defining f() in derived prevents 1 in base beingness considered during overload resolution.

you need explicitly bring base of operations definitions derived classes when want add together new overloads without hiding ones defined in base of operations classes.

public: using base::f;

c++ polymorphism object-slicing

No comments:

Post a Comment