C++ 比较从父类继承但存储在父类向量中的类的对象类型
我想比较从父类继承并存储在父类向量中的子类的对象类型,如下所示:C++ 比较从父类继承但存储在父类向量中的类的对象类型,c++,C++,我想比较从父类继承并存储在父类向量中的子类的对象类型,如下所示: #include <string> #include <iostream> #include <vector> #include <typeinfo> using namespace std; class Agent{ public: Agent(string nam){ name = nam; } ~Agent(); protected: string n
#include <string>
#include <iostream>
#include <vector>
#include <typeinfo>
using namespace std;
class Agent{
public:
Agent(string nam){ name = nam; }
~Agent();
protected:
string name;
};
class Human :public Agent{
public:
Human(string nam, int a):Agent(nam){ age = a; }
~Human();
protected:
int age;
};
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);
cout << (typeid(agents[1]) == typeid(Agent*)) << endl; /// True
cout << (typeid(agents[1]) == typeid(Human*)) << endl; /// I expect it to be true but its false
cout << (typeid(agents[1]) != typeid(Agent*)) << endl; /// False
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
类代理{
公众:
代理(字符串nam){name=nam;}
~Agent();
受保护的:
字符串名;
};
类别:公共代理人{
公众:
人类(字符串nam,int a):代理(nam){age=a;}
~Human();
受保护的:
智力年龄;
};
int main(){
载体制剂;
代理*代理=新代理(“ask”);
人类*人类=新人类(“ask2”,18);
Agent*agent2=新人类(“agentTask”,20);
代理。推回(代理);
代理。推回(人类);
代理。推回(代理2);
您创建了一个Agent*
的向量,因此(typeid(agents[1])==typeid(Human*))
为假,因为agents[1]
是代理
,而不是人
,这是一种在运行时区分层次结构中类型的可能方法(代码中的注释,如OP所要求):
#包括
#包括
//这是一个充当计数器的简单类
结构碳纳米管{
静态int-cnt;
};
int Cnt::Cnt=0;
//模板类帮助我们区分
//类型,并为它们提供一组值
//在运行时标识实际类型的
模板
结构类型:私有Cnt{
静态常数int型;
};
模板
常量int Type::Type=Cnt::Cnt++;
//代理提供了一个虚拟方法
//返回类型的数字标识符。
//使用上述类
//为该类型生成唯一值。
类代理{
公众:
虚int type()常量{
返回类型您可以找到:
无法保证同一类型上的typeid表达式的所有求值都会引用相同的std::type_info
实例
即使使用不同的类型,您也不会得到保证。无论如何,您每次都在同一类型上使用typeof
运算符。您可以对类使用类型特征,但如果您需要一个简单(快速且肮脏)的解决方案,您可以按如下方式执行:
#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
类代理{
公众:
静态常量字符串类型;
显式代理(conststring&nam):名称(nam){}
虚拟~Agent(){}
虚拟字符串类型()常量{
返回类型;
}
受保护的:
字符串名;
};
常量字符串代理::TYPE=“代理”;
类别:公共代理人{
公众:
静态常量字符串类型;
人类(const string&nam,int a):代理(nam),年龄(a){}
~Human(){}
虚拟字符串类型()常量{
返回类型;
}
受保护的:
智力年龄;
};
常量字符串Human::TYPE=“Human”;
int main(){
载体制剂;
代理*代理=新代理(“ask”);
人类*人类=新人类(“ask2”,18);
Agent*agent2=新人类(“agentTask”,20);
代理。推回(代理);
代理。推回(人类);
代理。推回(代理2);
对于(自动代理:代理){
cout类型()你不想比较它们,你想知道一个实例的实际类型。对吗?向量agents
的所有元素都是agents*
类型,即使你按Human*
我想知道一个实例的实际类型。@EissaN.你能解决这个问题吗it@ASK检查我的答案。这根本不能回答问题e OP要求一个有效的解决方案。@问是的,将struct-Agent
转换为class-Agent
,添加public
说明符,然后自己尝试一下。这是有效的。是否要我在示例中更改它们?完成。
#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;
}