比较从父类继承但存储在父类向量中的类的对象类型

时间:2016-05-22 07:26:06

标签: c++

我想比较从父类继承并存储在父类向量中的子类的对象类型,如下所示:

src="'myPartialTemplate.html'"

我需要帮助才能获得正确的结果 我搜索了它但找不到合适的解决方案和解释 请尝试尽可能多地解释您的代码。

3 个答案:

答案 0 :(得分:1)

这是一种在运行时区分层次结构中类型的可能方法(代码中的注释,如OP所请求的):

#include<vector>
#include<cassert>

// This is a simple class that acts as a counter

struct Cnt {
    static int cnt;
};

int Cnt::cnt = 0;

// A template class helps us to differentiate
// the types and to give them a set of  values
// that identify the actual types at runtime

template<typename T>
struct Type: private Cnt {
    static const int type;
};

template<typename T>
const int Type<T>::type = Cnt::cnt++;

// The Agent offers a virtual method that
// returns a numeric identifier of the type.
// The above mentioned classes are used
// to generate an unique value for this type.

class Agent {
public:
    virtual int type() const {
        return Type<Agent>::type;
    }
};

// If you want Human to have its own
// numeric identifier, you can simply override
// the inherited method and return a different
// type.
// Using the Type class is still the right
// solution. It assures that the returned type 
// won't collide with the ones generated so
// far.

class Human: public Agent {
public:
    int type() const override {
         return Type<Human>::type;
    }
};

int main() {
    std::vector<Agent*> vec;
    vec.push_back(new Agent);
    vec.push_back(new Human);
    assert(vec[0]->type() == Type<Agent>::type);
    assert(vec[0]->type() != Type<Human>::type);
    assert(vec[1]->type() == Type<Human>::type);
    assert(vec[1]->type() != Type<Agent>::type);
}

这是非常具有侵略性的,但是这样你也可以决定不给孩子一个不同的类型。

关于typeid的说明 从here您可以找到:

  

无法保证同一类型的typeid表达式的所有评估都会引用相同的std::type_info实例

即使使用不同类型,您也不会有任何保证。无论如何,您每次都使用typeof运算符。

答案 1 :(得分:1)

您可以为类使用类型特征,但如果您需要一个简单(快速和肮脏,可能)的解决方案,您可以按如下方式执行:

#include <string>
#include <iostream>
#include <vector>
#include <typeinfo>
using namespace std;

class Agent{
public:
    static const string TYPE;
    explicit Agent(const string& nam) : name(nam) {}
    virtual ~Agent(){}

    virtual string type() const {
        return TYPE;
    }
protected:
    string name;
};

const string Agent::TYPE = "Agent";

class Human :public Agent {
public:
    static const string TYPE;
    Human(const string& nam, int a):Agent(nam), age(a) {}
    ~Human(){}

    virtual string type() const {
        return TYPE;
    }
protected:
    int age;
};

const string Human::TYPE = "Human";


int main(){
    vector<Agent*> agents;
    Agent* agent=new Agent("ask");
    Human* human=new Human("ask2",18);
    Agent* agent2=new Human("AgentAsk",20);
    agents.push_back(agent);
    agents.push_back(human);
    agents.push_back(agent2);

    for(auto agent : agents) {
        cout << agent->type() << " ";
        cout << boolalpha << (agent->type() == Agent::TYPE) << endl;
    }
    //free up memory allocated using new
    // or just use smart pointers

    return 0;
}

最好定义一个抽象类并将抽象(如type()方法)和其他细节向下移动到派生类。

答案 2 :(得分:-2)

您创建了Agent*的向量,因此(typeid(agents[1]) == typeid(Human*))为false,因为agents[1]Agent,而不是Human