So I have the following situation: I have a (C++) driver that is not in my control that I have the headers and library files for but no source code. I want to simulate the driver in my SW (not just for tests, but use the same code as for the actual implementation). It would be easy to just create a derived class from the base class and re-implement all the methods except none of the methods in the driver base are marked as virtual so I cannot inherit them and still use the same base type in my code. I cannot create a custom header just for the simulation version because I want to be able to use the same code for simulation or real driver. I'm a bit stumped, how do I create a derived class from this non-virtual base?
Asked
Active
Viewed 98 times
0
-
1*"I'm a bit stumped"* - So am I. Because I totally failed to understand what you wish to accomplish. – StoryTeller - Unslander Monica Mar 05 '18 at 07:59
-
I have a base class Driver, which has no virtual methods. I have a program using the object via Driver* pointers. I want to implement a class SimulationDriver which inherits from Driver. Then in my software I could either use the real Driver or SimulationDriver with the same code. – Dago Mar 05 '18 at 08:04
-
1I read it the first time. Stating it again in a comment doesn't make it clearer I'm afraid. – StoryTeller - Unslander Monica Mar 05 '18 at 08:05
-
I'm afraid you can't. You can of course inherit from the base but you can't use the polymorphism feature. – xskxzr Mar 05 '18 at 08:07
-
To me it sounds as if what you are trying is just impossible. If there is no virtual function in the base, you can't reach the functions in the derived class using a base pointer – Support Ukraine Mar 05 '18 at 08:08
-
Possible duplicate of [Can I get polymorphic behavior without using virtual functions?](https://stackoverflow.com/questions/3818277/can-i-get-polymorphic-behavior-without-using-virtual-functions) – xskxzr Mar 05 '18 at 08:08
-
Clarify your question, not just to us, but also to yourself. By then, you may find that inheritance is the wrong way to go. – Mar 05 '18 at 08:08
-
For clarification: You want to be able to switch between the driver at run time? – Support Ukraine Mar 05 '18 at 08:18
-
1@Dago -- Maybe the original authors of the base class purposefully intended **not** for you to derive from it, which is why the functions are not virtual. – PaulMcKenzie Mar 05 '18 at 08:19
-
Is something forcing you to link the existing library files? If not, you can just re-implement them yourself. No need to template anything, just use the linker to provide your "virtual" dispatch. – Useless Mar 05 '18 at 08:35
1 Answers
2
If Driver
does not offer virtual functions, then having code which uses Driver *
use implementations from a class derived from Driver
is not possible.
However, you could change your code to be templated by the type of driver use and use two instantiations, one for Driver
and one for SimulationDriver
(which even wouldn't have to be derived from Driver
). Note that in this particular case (where you know all desired instantiations), you can sidestep the usual requirement of "all template code must be in header files" by using explicit instatiation. I use something similar in one of my projects.
Basically, code which currently looks like this:
file.hpp
class Driver;
void process(const Driver *d);
file.cpp
#include "file.hpp"
#include "Driver.h"
void process(const Driver *d)
{
d->doStuff();
}
will change to look like this:
file.hpp
template <class T_Driver>
void process(const T_Driver *d);
file.cpp
#include "file.hpp"
#include "Driver.h"
#include "SimulationDriver.h"
template <class T_Driver>
void process(const T_Driver *d)
{
d->doStuff();
}
template void process(const Driver *d);
template void process(const SimulationDriver *d);

Angew is no longer proud of SO
- 167,307
- 17
- 350
- 455