I have reference ref
:
Foo &ref = ..
I would like to call a method ref.say()
which is defined as virtual in Foo
and I know for sure it is overridden in child classes (because I wrote them as well).
However I would like to call say
as if it was not polymorphic, so the version which is defined in Foo, not in child classes.
How to do it?
One thing that comes to my mind is to take pointer of ref
, than dereference it and this trick should kill polymorphism, but I am not sure if this is guaranteed to take desired effect.
Please note, I am not sitting inside Foo or any of its child, the Foo tree is external structure from my current POV.
Calling virtual methods in constructor/destructor in C++ You can call a virtual function in a constructor. The Objects are constructed from the base up, “base before derived”.
When a method is declared as a virtual method in a base class and that method has the same definition in a derived class then there is no need to override it in the derived class.
C++ has access control, but not visibility control. This means that private functions are visible but not accessible. A private virtual function can be overridden by derived classes, but can only be called from within the base class.
A virtual function can be private as C++ has access control, but not visibility control. As mentioned virtual functions can be overridden by the derived class but under all circumstances will only be called within the base class. Example: C++
How about
ref.Foo::say();
This what you looking for?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With