First, let me preface this question with "I am new to C++"! This will probably turn out to be something very simple and I'll feel pretty dumb but right now I could really use the help. I know how to do this with an Abstract class but I can't use an Abstract class here. Also, if there is a better way to do this, I'm open to suggestions.
I have a ClientCommand (base class) and a MasterCommand (extends base class)
The MasterCommand can do everything that the ClientCommand can do and a few more methods.
In my application, I want to use the same name for both commands. If the object is a Client then it will be limited to only client methods. If the object is a Master then it will have all the client methods plus the Master methods.
I know there has got to be a way to do this.
- I am not casting it correctly
- I am not constructing it correctly
- Or there is something else I'm doing wrong
Here is my test code:
BASE CLASS HEADER:
#ifndef __OPCommand_H_
#define __OPCommand_H_
class OPCommand {
public:
virtual void doSomething();
void doClientStuff();
};
#endif //__OPCommand_H_
BASE CLASS BODY:
#include "OPCommand.h"
void OPCommand::doSomething() {
// TODO - implement OPClientCommand::doSomething
throw "Not yet implemented";
}
void OPCommand::doClientStuff() {
// TODO - implement OPClientCommand::doClientStuff
throw "Not yet implemented";
}
EXTENDED CLASS HEADER:
#ifndef __OPMasterCommand_H_
#define __OPMasterCommand_H_
#include "OPCommand.h"
class OPMasterCommand : public OPCommand {
public:
void doSomething();
void doMasterStuff();
};
#endif //__OPMasterCommand_H_
EXTENDED CLASS BODY:
#include "OPMasterCommand.h"
void OPMasterCommand::doSomething() {
// TODO - implement OPMasterCommand::doSomething
throw "Not yet implemented";
}
void OPMasterCommand::doMasterStuff() {
// TODO - implement OPMasterCommand::doMasterStuff
throw "Not yet implemented";
}
TEST CLASS HEADER:
#ifndef __TestMe_H_
#define __TestMe_H_
#include "OPMasterCommand.h"
#include "OPCommand.h"
class TestMe {
public:
OPCommand* command;
OPMasterCommand* masterCommand;
public:
void startHere();
OPMasterCommand* getMasterCommand();
OPCommand* getCommand();
bool isMaster();
};
#endif //__TestMe_H_
TEST CLASS BODY:
Pay close attention to the startHere method:
#include "TestMe.h"
void TestMe::startHere() {
if (isMaster()) {
masterCommand = getMasterCommand();
} else {
command = getCommand();
}
// Here - How can I point to or cast to the appropriate type, so I can use the same name everywhere in my code?
}
OPMasterCommand* TestMe::getMasterCommand() {
if (!masterCommand) {
masterCommand = new OPMasterCommand();
}
//command = dynamic_cast<OPCommand*> (masterCommand);
return masterCommand;
}
OPCommand* TestMe::getCommand() {
if (!command) {
command = new OPCommand();
}
return command;
}
bool TestMe::isMaster() {
return true;
}
I cannot get this to build. Right now I'm getting a redefinition of 'OPCommand'