Is it feasible in C++ to possess a class override an online function, only have virtual dispatch once the function is known as with the superclass (ie. not when it's known as on something statically typed because the subclass)? I understand this is not what goes on, but can there be in whatever way to attain something close?
The reason behind wanting this really is which i have two classes which both expose a
flush() function. The huge most of the amount of time in my program, I'm calling
flush() on a subclass object which i know the kind of, and so i have no need for virtual dispatch. However I wish to give a superclass in to the mix to ensure that very rarely I'm able to pass a mention of the a clear case of either among the classes right into a
doSomethingThenFlush() function, which may call
I understand I possibly could use templates rather than virtual functions, and that i know I possibly could have two different functions (eg.
flushVirtual() which just known as
flushNonVirtual(), and call
flushNonVirtual() everywhere I do not need virtual dispatch). However these both appear a little like tossing code in a problem that is largely syntactical. Can there be anymore elegant way to do this?
Possibly more to the point, does anybody know why virtualness comes in C++?
struct Base virtual void func() struct Derived : public Base void callVirtually(Base &base) void callStatically(Derived &derived) int primary()
Virtualness comes because you do not know if a person will derive further out of your
Derived. Someone could equally well create a
MoreDerived that could be passed to some function expecting a
Derived&lifier, and they might be sad once they discovered that it had been
Derived's versions of your virtual functions were bieng known as rather than
Should you imply that you will not ever inherit from
Derived so you won't want to purchase an online function call, then you are at a complete loss, because C++ provides not a way to promise that you simply will not ever inherit from the class, which may be essential to do what you're wanting.
The solution is based on your question.
derived.func() // no virtual dispatch
Whenever you call a
virtual function using object, there's no
virtual dispatch. It calls the function while using static-kind of the item.
virtual function makes the image only if you attempt to call a function utilizing a pointer or perhaps a reference.
Edit: Inside your up-to-date question, you're using
Derived&lifier to call
func(). Calling by reference will make certain that
virtual dispatch happens. So there's no language facility (like
final in Java), that will stop
Inside your specific example, if
callStatically will get inlined, the compiler will most likely steer clear of the virtual function dispatch since it can easily see the object's actual type (since it is a nearby variable).
Most likely your compiler may also avoid an online dispatch for cases such as this:
class Foo private: Derived d
The compiler will probably perform this optimisation whether
callStatically is inlined, since it can easily see the particular kind of the variable member.
But to my understanding, there's not a way to pressure the compiler to bypass the virtual get in touch with general.