Non virtual pointer in c++?

Viewed 112

Is it possible to have a “non virtual pointer” in c++ ( ie a pointer that calls the methods directly without dynamic dispatch). This is because I am trying to do something like this:

template <typename T>
class Ref {
    T* value; // I am 100% sure that value points to an actual T object.
    /* constructors and stuff */
    auto operator -> () {
        return value;
    }
};

But this will unnecessarily call virtual functions when I know the dynamic type of value to be T... I know that the user could enforce the usage of non-virtual functions like so:

X->Base::foo();

But that seems like another burden for the user. How can this be done automatically?

2 Answers

when I know the dynamic type of value to be T

You may know/assume this, but at present, there is no mechanism in C++ that can make the language know/assume this for any given pointer/reference to an object. If it is of a polymorphic type, then calling any virtual function (unless the caller explicitly specifies otherwise) will use dynamic dispatch.

Related