Due to my device I can not use virtual functions. Suppose I've:

class Base

class Derived : public Base

// anywhere

the obj->doSomething() will call only the Base::doSomething()

It is possible to way with Base *obj, to call the doSomething from the Derived?

I understand I'm able to just put a virtual before doSomething() of Base it solve the issue, but I am restricted to my device, the compiler does not support it.

You can lower cast the bottom class pointer towards the derived class and call the function.

Base* obj = new Derived

Derived* d = static_cast<Derived*>( obj )


Since doSomething() isn't declared virtual, you need to get the derived implementation.

You are able to downcast the item towards the Derived type and refer to it as, like so:


though that doesn't afford any guarantees that what 'obj' indicates is really of type Derived.

I am more concerned you don't even get access to virtual functions. How can destructors work if none of the functions could be virtual, and you're simply subclassing?

Sure this can be done it is simply not always easy.

If there's a finite listing of derived classes and guess what happens they're whenever you define the bottom class, this can be done utilizing a non-polymorphic member function wrapper. Here's a good example with two derived classes. It uses no standard library facilities and relies exclusively on standard C++ features.

class Base

class Derived1

class Derived2

class MemFnWrapper

    typedef void(Base::*BaseFnType)()

    typedef void(Derived1::*Derived1FnType)()

    typedef void(Derived2::*Derived2FnType)()

    MemFnWrapper(BaseFnType fn) : type_(BaseType) 

    MemFnWrapper(Derived1FnType fn) : type_(Derived1Type) 

    MemFnWrapper(Derived2FnType fn) : type_(Derived2Type) 

    void operator()(Base* ptr) const


    union FnUnion


    DerivedType type_

    FnUnion fn_

class Base

    Base(MemFnWrapper::Derived1FnType f) : doSomethingImpl(f) 

    Base(MemFnWrapper::Derived2FnType f) : doSomethingImpl(f) 

    void doSomething() 


    void myDoSomething() 

    MemFnWrapper doSomethingImpl

class Derived1 : public Base


    void myDoSomething() 

class Derived2 : public Base


    void myDoSomething() 

// Complete the MemFnWrapper function call operator this needs to be following the

// definitions of Derived1 and Derived2 therefore the cast applies:

void MemFnWrapper::operator()(Base* ptr) const

situation BaseType:     return (ptr->*(fn_.baseFn_))()

    situation Derived1Type: return (static_cast<Derived1*>(ptr)->*(fn_.derived1Fn_))()

    situation Derived2Type: return (static_cast<Derived2*>(ptr)->*(fn_.derived2Fn_))()


int primary()

(I initially recommended using std::function, which does lots of this meet your needs, however I appreciated it's a polymorphic function wrapper, therefore it always uses virtual functions. :-P Oops. You will see the revision history to determine what that certain appeared as if)