大家好,C ++如何检查类对象不是指定的类[结果发布] [英] hello everyone,c++ how check class object is not a specified class[results posted]
本文介绍了大家好,C ++如何检查类对象不是指定的类[结果发布]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
class MyString
{
public:
MyString();
};
class MyStringA
{
public :
MyStringA();
}
int main()
{
MyString *pStr=new MyString;
MyStringA *pStrA=new MyStringA;
if(pStrA==Mystring) ??????how check pStrA class object belong MyString Class object
{
cout << "Be MyString"<<endl;
}
else
{
cout << "not MyString"<<endl;
}
}
englist只有穷人,谢谢回答!
c ++ fan!;
englist only poor,Thank you to answer!
c++ fan!;
推荐答案
使用rtti.您可能正在使用typeid或dyn cast
Use rtti. U will perhaps be using typeid or dyn cast
检查 C ++运行时类型信息
#include <iostream>
#include <typeinfo>
using namespace std;
// Debug in
// Code::Blocks 10.05
// GCC 4.4.1
// 2012-2-18
class MyString
{
public:
MyString(){} // Default Implementation
};
class MyStringA
{
public :
MyStringA(){} // // Default Implementation
}; // you forgot a semicolon here
int main()
{
MyString *pStr = new MyString;
MyStringA *pStrA = new MyStringA;
if(typeid(*pStrA) == typeid(MyStringA)) // do the check
{
cout << "Be MyString"<<endl;
}
else
{
cout << "not MyString"<<endl;
}
}
这篇关于大家好,C ++如何检查类对象不是指定的类[结果发布]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文