An abstract class inheriting from another abstract class with the same function name

class subscriber { public: virtual void update() = 0; } class entity : public subsriber { public: virtual void update() = 0; } class myObject : public entity { public: virtual void update() { do_things(); } } subscriber * ptr = new myObject; //will use shared_ptr, but here i want simplicity ptr->update(); 

The question is, should the proper update function be called (implemented in myObject)? And are there really two pure virtual functions with the same name in the same "family"?

+4
source share
2 answers

will the proper update function be called (implemented in myObject)?

Yes, it will be called.

Does it really have two pure virtual functions with the same name in the same "family"?

The second declaration (i.e., inside the entity class) does not introduce a second pure virtual function into the family: the signatures are identical, therefore update() is the only virtual function. Moreover, declaring it for the second time is not required: entity will remain abstract and will have access to the update() method, even if you delete the second declaration.

+5
source

A virtual function or virtual method is a function or method whose behavior can be redefined within an inheriting class by a function with the same signature.

So the answer is yes.

+1
source

All Articles