考虑以下 C 代码:
class Instruction {
public:
virtual void execute() { }
};
class Add: public Instruction {
private:
int a;
int b;
int c;
public:
Add(int x, int y, int z) {a=x;b=y;c=z;}
void execute() { a = b c; }
};
在一个单独的类中:
void some_method() {
vector v;
Instruction* i = new Add(1,2,3)
v.push_back(*i);
}
在另一个类中:
void some_other_method() {
Instruction ins = v.back();
ins.execute();
}
这些类共享指令向量。但是,我们遇到了有关执行函数的问题。它会保留其 Add 类型吗?
不幸的是,它不会。 vector
要解决此问题,请考虑使用 vector
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3