2

这可能是我想要实现的目标吗?这就是问题:

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 content1orcontent2类型,因此 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;
}
4

4 回答 4

2

如果你的类型是多态的(即如果它们至少有一个virtual函数),你可以使用dynamic_cast<>. 如果指向的对象实际上不是指定类或其派生类的实例,则结果dynamic_cast<>将为空指针(即nullptr,对于 C++11,对于 C++03):NULL

Container cont;
Content1* pContent1 = dynamic_cast<Content1*>(cont.c);
if (pContent1 != nullptr)
{
     // cont.c points to an instance of Content1 
     // or a class derived from Content1
}

一种常见的做法是使基类的析构函数成为虚拟的,以便从基类派生的类的对象可以delete通过基类指针进行 d(如果析构函数不是virtual,则尝试这样做会导致未定义的行为):

class Content //Abstract.
{
public:
    virtual ~Content() { }
//  ^^^^^^^
};
于 2013-03-30T18:26:01.873 回答
1

是的,使用dynamic_cast- http://www.cplusplus.com/doc/tutorial/typecasting/

它将为您提供 RTTI - 运行时类型信息。

Content1 * p = dynamic_cast<Content1 *>(c);

if p is non-NULL, then c points to an object which is Content1 or derived from Content1. However, your class heirarchy needs to be polymorphic. Since your class Content is Abstract (I am assuming you have a pure virtual function in Content), your whole heirarchy automatically is polymorphic.

于 2013-03-30T18:26:52.467 回答
1

If you have to know the exact runtime type of a polymorphic class, you should rethink your design. Usually it is better to either keep objects of different types in different containers (additionally or, instead of one central container) or add a virtual function to each of the derived classes which performs the action where you would have to discriminate between the types.

There is an entry in the C++ FAQ which might help you: I have a heterogeneous list of objects, and my code needs to do class-specific things to the objects. Seems like this ought to use dynamic binding but can't figure it out. What should I do?

于 2013-03-30T18:42:49.753 回答
0

If you want to see if Content* points to an object of a class derived from Content2 class just cast it with dynamic_cast and see.

This works:

#include <iostream>
#include <typeinfo>

using namespace std;

class Content{public: virtual ~Content(){}};

class Content1 : public Content{public: virtual ~Content1(){}};
class Content1_1 : public Content1{public: virtual ~Content1_1(){}};
class Content1_2 : public Content1{public: virtual ~Content1_2(){}};

class Content2 : public Content{public: virtual ~Content2(){}};
class Content2_1 : public Content2{public: virtual ~Content2_1(){}};
class Content2_2 : public Content2{public: virtual ~Content2_2(){}};

class Container{
public:
    Content* c;
};

int main()
{
    Content* cnt=new Content2_1;
    if(dynamic_cast<Content2_1*>(cnt))
        cout << "True" << endl;
    return 0;
}
于 2013-03-30T18:53:47.977 回答