I would like to have a C++ Interface that must be overridden (if this is possible) when inherited. So far, I have the following:
class ICommand{
public:
// Virtual constructor. Needs to take a name as parameter
//virtual ICommand(char*) =0;
// Virtual destructor, prevents memory leaks by forcing clean up on derived classes?
//virtual ~ICommand() =0;
virtual void CallMe() =0;
virtual void CallMe2() =0;
};
class MyCommand : public ICommand
{
public:
// Is this correct?
MyCommand(char* Name) { /* do stuff */ }
virtual void CallMe() {}
virtual void CallMe2() {}
};
I have purposely left how I think the constructor/destructor’s should be implemented in ICommand. I know if I remove the comments, it will not compile. Please could someone:
- Show me how to declare the constructor/destructor’s in
ICommandand how they are meant to be used inMyCommand - Have I set things up correctly in
ICommandso thatMyCommandmust overrideCallMeandCallMe2.
C++ does not allow for virtual constructors. A simple implementation (without the virtual constructor) would look something like this:
Note that even a pure virtual destructor must be defined.
A concrete implementation would look exactly like your example:
You have a couple of options for the constructor. One option is to disable the default constructor for
ICommand, so that subclasses will have to implement a constructor that calls your ICommand constructor:A concrete implementation would now look something like this: