Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22153

Is it possible to use a derived class virtual method from a base class non-virtual method?

$
0
0

I have multiple derived classes which extends the same Base class. They have the same properties, with the exception of a single method that might have different arguments and body. How can I have a std::unique_ptr and call methods for the derived classes?

Here is what I know so far:

  1. A function template can not be virtual;
  2. A pure virtual function can not be overriden with a different signature;
  3. A function pointer would require to know the argument list of all possible derived functions

Here is what I'm currently leaning torwards (without success):

Base class:

class IBase{  /** ... **/  template <class T, typename ...Args>  MyCustomType perform(T &obj, Args ... args)  {    std::bind(obj->run, args);  }  virtual MyCustomType run(...) = 0;};

Derived class:

class Derived : IBase{  /** ... **/  virtual MyCustomType run(MyType1 a, MyType2 b, bool c) override  {    /* code */  }};

I wanna be able to use my pointer to the base class, call basePtr->perform(a, b, c) and the method from subclass Derived to be executed. Does it make any sense?


Viewing all articles
Browse latest Browse all 22153

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>