C++ C++;如何扩展类并强制转换为具有相同名称的适当类型
首先,让我以“我是C++新手”作为这个问题的开场白!这可能会变成一件非常简单的事情,我会觉得很傻,但现在我真的需要帮助。我知道如何使用抽象类,但我不能在这里使用抽象类。此外,如果有更好的方法,我愿意接受建议C++ C++;如何扩展类并强制转换为具有相同名称的适当类型,c++,polymorphism,C++,Polymorphism,首先,让我以“我是C++新手”作为这个问题的开场白!这可能会变成一件非常简单的事情,我会觉得很傻,但现在我真的需要帮助。我知道如何使用抽象类,但我不能在这里使用抽象类。此外,如果有更好的方法,我愿意接受建议 我有一个ClientCommand(基类)和一个MasterCommand(扩展基类) MasterCommand可以完成ClientCommand所能做的一切,还可以使用其他一些方法 在我的应用程序中,我希望两个命令使用相同的名称。如果对象是客户机,那么它将仅限于客户机方法。如果对象是
我有一个ClientCommand(基类)和一个MasterCommand(扩展基类) MasterCommand可以完成ClientCommand所能做的一切,还可以使用其他一些方法 在我的应用程序中,我希望两个命令使用相同的名称。如果对象是客户机,那么它将仅限于客户机方法。如果对象是主对象,那么它将拥有所有客户端方法和主方法 我知道一定有办法做到这一点
基类标题:
#ifndef __OPCommand_H_
#define __OPCommand_H_
class OPCommand {
public:
virtual void doSomething();
void doClientStuff();
};
#endif //__OPCommand_H_
#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_
基类主体:
#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";
}
#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";
}
扩展类标题:
#ifndef __OPMasterCommand_H_
#define __OPMasterCommand_H_
#include "OPCommand.h"
class OPMasterCommand : public OPCommand {
public:
void doSomething();
void doMasterStuff();
};
#endif //__OPMasterCommand_H_
扩展类主体:
#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";
}
#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";
}
测试类标题:
#ifndef __OPCommand_H_
#define __OPCommand_H_
class OPCommand {
public:
virtual void doSomething();
void doClientStuff();
};
#endif //__OPCommand_H_
#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_
测试类主体:
#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";
}
#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";
}
请密切注意startHere方法:
#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;
}
#包括“TestMe.h”
void TestMe::startHere(){
if(isMaster()){
masterCommand=getMasterCommand();
}否则{
command=getCommand();
}
//这里-如何指向或强制转换到适当的类型,以便在代码中的任何地方都可以使用相同的名称?
}
OPMasterCommand*TestMe::getMasterCommand(){
如果(!masterCommand){
masterCommand=新的OPMasterCommand();
}
//命令=动态施法(主命令);
返回主命令;
}
OPCommand*TestMe::getCommand(){
如果(!命令){
command=new OPCommand();
}
返回命令;
}
booltestme::isMaster(){
返回true;
}
我无法建造这个。现在我重新定义了“op司令”< p> >我的心理调试技能告诉我,因为你是C++新手,你忘记了你的头包含守护程序(这是代码的一个非常常见的原因,重新定义‘op司令’< /Cord>你看到的错误)。 尝试在头文件中添加类似的内容(更改每个头的名称):
#ifndef OPCOMMAND_H_included
#define OPCOMMAND_H_included
<original header contents>
#endif
\ifndef OPCOMMAND\u包括在内
#定义包含的OPU命令
#恩迪夫
回答问题的第二部分:如何为所有命令使用单个基类和单个变量
你需要查一下图案。在本例中,您需要从命令派生所有类
此外,如果您从ClientCommand派生MasterCommand,则不需要多重继承:Command请显示所需类继承权的图表。首先,请显示完整的错误消息和受影响的行。另外,看起来您在TestMe中两次包含OPCommand.h,并且OPCommand.hAlso中没有保护定义,您需要在“command”标题中使用ifndef/define/#endif。@Arkadiy-ifndef/define/#endif在“command”标题中消除了错误,所以现在我正在构建。通常,当您有命令模式时,您有一个命令::doSomething()和3个命令类:ClientCommand、MasterCommand和OtherCommand。然后通过命令引用/指针访问所有3个。或者,如果编译器足够新,您可以添加一个组合,将客户端和Common under 1 CommandOr组合起来。
\pragma once
@Fredrarson:我认为这些答案没有提到\pragma once
的大问题。有关何时#pragma once
可能失败的信息,请参阅。@RedX:这不是编译器是否足够新的问题<代码>#pragma once无法在所有情况下正确实施,而传统的include防护始终是安全的。看看我上面贴的链接。嘿,马克:谢谢你的提示,你说得对。但我真正的问题是我想在代码中使用相同的名称(即cmd)。出现问题的原因是有两种不同的类型:OPClientCommand和OPMasterCommand。我怎样才能使cmd成为任意一个类?