use*_*514 1 c++ inheritance class copy-constructor virtual-copy
嗨,我有两个类,一个叫做Instruction,一个叫做LDI,它继承自指令类.
class Instruction{
protected:
string name;
int value;
public:
Instruction(string _name, int _value){ //constructor
name = _name;
value = _value;
}
~Instruction(){}
Instruction (const Instruction &rhs){
name = rhs.name;
value = rhs.value;
}
void setName(string _name){
name = _name;
}
void setValue(int _value){
value = _value;
}
string getName(){
return name;
}
int getValue(){
return value;
}
virtual void execute(){}
virtual Instruction* Clone() {
return new Instruction(*this);
}
};
/////////////end of instruction super class //////////////////////////
class LDI : public Instruction{
void execute(){
//not implemented yet
}
virtual Instruction* Clone(){
return new LDI(*this);
}
};
Run Code Online (Sandbox Code Playgroud)
然后我创建一个类型为Instruction的指针,并尝试指向LDI类型的新实例.
Instruction* ptr;
ptr = new LDI("test", 22);
Run Code Online (Sandbox Code Playgroud)
我得到以下编译器错误.我有什么想法我做错了吗?
functions.h:71: error: no matching function for call to ‘LDI::LDI(std::string&, int&)’
classes.h:54: note: candidates are: LDI::LDI()
classes.h:54: note: LDI::LDI(const LDI&)
Run Code Online (Sandbox Code Playgroud)
代码:new LDI(name, val)具体说"使用name和调用LDI构造函数val".
没有LDI构造函数name / val.事实上,我根本没有看到LDI的构造函数.
如果要使用基类的构造函数,请按以下步骤操作:
public LDI(string _name, int _value) // Public constructor for LDI
: Instruction(_name, _value) // Delegate to the base-class constructor
{
// Do more LDI-specific construction here
}
Run Code Online (Sandbox Code Playgroud)