Does C ++ have a static polymorphism of an interface implementation that does not use vtable?

Does C ++ have the correct implementation of an interface that does not use vtable?

eg

class BaseInterface{ public: virtual void func() const = 0; } class BaseInterfaceImpl:public BaseInterface{ public: void func(){ std::cout<<"called."<<endl; } } BaseInterface* obj = new BaseInterfaceImpl(); obj->func(); 

the func call in the last line goes to vtable to find func ptr from BaseInterfaceImpl :: func, but is there any C ++ way to do this directly since BaseInterfaceImpl is not subclassed from any class other than the pure BaseInterface interface class?

Thanks. Gil.

+6
c ++ interface vtable
source share
2 answers

Yes. He clicks on CRTP . Have a gander.

+7
source share

I think that in any language you will need to go to some equivalent vtable for dynamic sending, unless at compile time it is known which function to call. This may be the result of smart optimization of the compiler or a method such as CRTP (which was already mentioned).

0
source share

All Articles