I have an abstract base class, and multiple objects that might inherit from it. For instance
class Mode {
public:
int n,l,m;
double omega;
virtual int doSomething() =0;
virtual ~Mode(){};
}
class ModeA : Mode {
ModeA(int N, int L, int M);
ModeA(double W, int L, int M);
virtual ~ModeA();
int doSomething();
}
class ModeB : Mode {
ModeB(int N, int L, int M);
ModeB(double W, int l, int M);
virtual ~ModeB();
int doSomething();
}
The two child classes, ModeA
and ModeB
have constructors with parameters (int, int, int)
and (double, int, int)
. Later, I have a piece of code that expects as Mode
object, which looks like
template <class MODE> int mode_finder(MODE** mode_list, int num_mode, int N, int L, int M){
assert((std::is_base_of<Mode,MODE>::value));
for(int i=0; i<num_mode; i++){
mode_list[i] = new MODE(N,L,M);
... some more code here ...
}
}
(this isn't simply a function to initialize a list of modes, the ...some more code here...
part is about 300 lines long and includes checking values against a list and deleting and recreating if they don't match -- a lot of stuff has been left out of this simple working example)
If I'm using ModeA
and ModeB
, then this is fine, because they have the right kind of constructors.
My problem is that I (or whoever inherits this code) might eventually make a new type and forget to put in constructors of this form, such as
class ModeC : Mode {
ModeC(int X);
virtual ~modeC();
int doSomething();
}
This will create run-time errors if I call mode_maker
with ModeC
, when the function template for ModeC
is created.
I want for every object to inherit from Mode
to implement constructors with two signatures: a (int, int, int)
and (double, int, int)
form. If not, I want to throw a compile-time error.
I assumed pure virtual constructors would do the trick, something like:
virtual Mode(int, int, int) =0;
virtual Mode(double, int, int) =0;
but apparently that isn't a language feature due to vtable issues. I don't really need entries on the vtable for anything, I just need the child classes to be forced to make constructors of this type.
How can I do the thing I'm trying to do?
Thanks in advance.