我需要在方法中初始化一个对象,而不指定对象所在的类。我可以这样做吗?
有人能给我举个例子吗?
编辑:
MyClass{
...};
MySecondClass
{...
};
void method(*object); //how to write correct??
{..}
MyClass *x= new MyClass();
MySecondClass *y= new MySecondClass();
method(x);
method(y);
最佳答案
使用模板。
template <typename T>
void method(T* object) {
// do stuff with the object, whose real type will be substituted for `T`
}
模板有点复杂,因此请阅读 C++ 书籍中有关它们的章节以获取更多信息。
关于c++ - 2个不同的类如何指向同一个数据表名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5910888/