Override a virtual member in a instance?

T

tony

Hi!
A derived class can override a method in the base class it inherits for,
and even my dog knows that. More incredibly, I know and understand it
too.

But, can a (of course virtual in this case) method be overriden in an
instance instead? For example, to provide some callbacks to a class.

Should I use virtual functions instead? How do I pass the this pointer
when casting it from or to in any useful way seems to break every C++
language rule? As a normal parameter? But then it doesn't really look
so OOP anymore.

Thanks a lot,
Tony
 
V

Victor Bazarov

A derived class can override a method in the base class it inherits
for, and even my dog knows that. More incredibly, I know and
understand it too.

But, can a (of course virtual in this case) method be overriden in an
instance instead? For example, to provide some callbacks to a class.

No. For that you usually store pointers to [member] functions and assign
them different values based on the need, per instance.
Should I use virtual functions instead?

Instead of what? Instead of virtual functions? Uh... Yes!.. I mean,
no... I mean... Could you rephrase, please?
How do I pass the this pointer
when casting it from or to in any useful way seems to break every C++
language rule? As a normal parameter? But then it doesn't really look
so OOP anymore.

Huh? I think I don't understand (where does it place me relative to
your dog?), could you give an example in C++?

V
 
K

Kai-Uwe Bux

Hi!
A derived class can override a method in the base class it inherits for,
and even my dog knows that. More incredibly, I know and understand it
too.

But, can a (of course virtual in this case) method be overriden in an
instance instead? For example, to provide some callbacks to a class.

Should I use virtual functions instead? How do I pass the this pointer
when casting it from or to in any useful way seems to break every C++
language rule? As a normal parameter? But then it doesn't really look
so OOP anymore.

Virtual functions are type bound (although to the dynamic type). You can use
a function pointer or a member variable whose type is a function object if
you want to override the semantics of the call per instance.



Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top