我有 2 个类:
class Entity {
void addChild(Entity* e);
};
class Control : public Entity {
};
我想做的是不允许将控件添加为非控件的子项。所以,例如:
Control c;
Entity e;
e.addChild(c); // This line would throw an error (at compile time if possible);
我首先想到的是将其添加到实体中:
void addChild(Control* c){
assert(false);
};
注意:Entity 和Control 都是抽象类,但都有很多子类。
但是有什么方法可以在编译时得到错误吗?
最佳答案
您可以声明函数,但不要实现它。如果您尝试使用它,将会出现链接器错误。
关于c++ - 在 C++ 中是否可以禁止在编译时调用某个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7524634/