这可能是我想要实现的目标吗?这就是问题:
class Content{};
class Content1 : public Content{};
class Content1_1 : public Content1{};
class Content1_2 : public Content1{};
class Content2 : public Content{};
class Content2_1 : public Content2{};
class Content2_2 : public Content2{};
class Container{
public:
Content* c;
};
是否可以确定Container
类的指针指向的对象Content1
或派生自的类的对象Content1
?
谢谢。
编辑:
如果我在类中查找指针的container
类型,它被视为一种content
类型,尽管它实际上可能是 a content1
orcontent2
类型,因此 dynamic_cast 不起作用。
class content{};
class content1 : public content{};
class content2 : public content{};
class container{
public:
content* c;
};
int main(void)
{
container* x = new container;
x->c = new content2;
if( dynamic_cast<content1*>((content1*)x->c) == NULL){
//This doesn't fail, eventhough 'content1' and 'content2' shouldn't be compatible.
//This means that every class, derived from 'content' will be treated as 'content1' when using dynamic_cast.
}
return 0;
}