标签:rtu 编译器 ESS 运算 vat code dba 静态 ide
Run Time Type Identification:运行时类型识别;通过运行时类型识别,程序能够使用基类的指针或引用来检查这些指针或引用所指向的对象的实际派生类型。
Human *phuman = new Men;
Human &q = *phuman;
主要是通过两个运算符来实现的:
#include <iostream>
using namespace std;
class Human
{
public:
Human();
Human(int);
virtual ~Human();
public:
int m_Age;
char m_Name[100];
public:
virtual void eat() =0;
};
Human::Human()
{
cout << "Human::Human()" << endl;
}
Human::~Human()
{
cout << "Human::~Human()" << endl;
}
Human::Human(int tmp)
{
cout << "Human::Human(int tmp)" << endl;
}
//Men是Human的子类
class Men:public Human
{
public:
Men();
Men(int);
~Men();
private:
public:
virtual void eat();
};
Men::Men()
{
cout << "Men::Men()" << endl;
}
Men::~Men()
{
cout << "Men::~Men()" << endl;
}
Men::Men(int tmp)
{
cout << "Men::Men(int tmp)" << endl;
}
void Men::eat()
{
cout << "men eat mi" << endl;
}
//Women是Human的子类
class Women :public Human
{
public:
Women();
Women(int);
~Women();
private:
public:
virtual void eat();
};
Women::Women()
{
cout << "Women::Women()" << endl;
}
Women::~Women()
{
cout << "Women::~Women()" << endl;
}
Women::Women(int tmp)
{
cout << "Women::Women(int tmp)" << endl;
}
void Women::eat()
{
cout << "women eat miantiao" << endl;
}
int main()
{
//Men men;
Human* p_m = new Men;
Men* p_men = dynamic_cast<Men*>(p_m);
if (p_men != nullptr)
cout << "cast success! p_men is Men*" << endl;
else
cout << "cast fail! p_men is not Men*" << endl;
}
#include <iostream>
using namespace std;
class Human
{
public:
Human();
Human(int);
virtual ~Human();
public:
int m_Age;
char m_Name[100];
public:
virtual void eat() =0;
};
Human::Human()
{
cout << "Human::Human()" << endl;
}
Human::~Human()
{
cout << "Human::~Human()" << endl;
}
Human::Human(int tmp)
{
cout << "Human::Human(int tmp)" << endl;
}
//Men是Human的子类
class Men:public Human
{
public:
Men();
Men(int);
~Men();
private:
public:
virtual void eat();
};
Men::Men()
{
cout << "Men::Men()" << endl;
}
Men::~Men()
{
cout << "Men::~Men()" << endl;
}
Men::Men(int tmp)
{
cout << "Men::Men(int tmp)" << endl;
}
void Men::eat()
{
cout << "men eat mi" << endl;
}
//Women是Human的子类
class Women :public Human
{
public:
Women();
Women(int);
~Women();
private:
public:
virtual void eat();
};
Women::Women()
{
cout << "Women::Women()" << endl;
}
Women::~Women()
{
cout << "Women::~Women()" << endl;
}
Women::Women(int tmp)
{
cout << "Women::Women(int tmp)" << endl;
}
void Women::eat()
{
cout << "women eat miantiao" << endl;
}
int main()
{
//引用
Human* phuman = new Men;
Human& q = *phuman;
try
{
Men& m = dynamic_cast<Men&>(q);
}
catch(std::bad_cast) //转换不成功
{
cout << "p is not Men!" << endl;
}
return 0;
}
typeid(类型[指针/引用]/表达式);//得到对象类型信息
typeid会返回一个常量对象的引用,这个常量对象是一个标准库类型type_info(类/类类型);
#include <iostream>
using namespace std;
class Human
{
public:
Human();
Human(int);
virtual ~Human();
public:
int m_Age;
char m_Name[100];
public:
virtual void eat() =0;
};
Human::Human()
{
cout << "Human::Human()" << endl;
}
Human::~Human()
{
cout << "Human::~Human()" << endl;
}
Human::Human(int tmp)
{
cout << "Human::Human(int tmp)" << endl;
}
//Men是Human的子类
class Men:public Human
{
public:
Men();
Men(int);
~Men();
private:
public:
virtual void eat();
};
Men::Men()
{
cout << "Men::Men()" << endl;
}
Men::~Men()
{
cout << "Men::~Men()" << endl;
}
Men::Men(int tmp)
{
cout << "Men::Men(int tmp)" << endl;
}
void Men::eat()
{
cout << "men eat mi" << endl;
}
//Women是Human的子类
class Women :public Human
{
public:
Women();
Women(int);
~Women();
private:
public:
virtual void eat();
};
Women::Women()
{
cout << "Women::Women()" << endl;
}
Women::~Women()
{
cout << "Women::~Women()" << endl;
}
Women::Women(int tmp)
{
cout << "Women::Women(int tmp)" << endl;
}
void Women::eat()
{
cout << "women eat miantiao" << endl;
}
int main()
{
//引用
Human* phuman = new Men;
Human& q = *phuman;
cout << typeid(*phuman).name() << endl;
cout << typeid(q).name() << endl;
//1. typeid判断两个指针指向的是否是同一种类型的对象
Human* phuman_1 = new Men;
Human* phuman_2 = new Women;
//比较对象,看的是new出来的是哪个对象或者该指针指向的是哪个对象,和定义该指针是定义的类型无关;
//不要忘记*,解引用;
if (typeid(*phuman_1) == typeid(*phuman_2)) //不成立
cout << "same!" << endl;
else
cout << "not same!" << endl;
if (typeid(*phuman_1) == typeid(Men))
cout << "puhuman_1 is Men" << endl;
//基类必须要有虚函数,否则上述条件不成立;
//如果基类中不含虚函数,那么typeid返回的是表达式的静态类型;
return 0;
}
typeid就返回一个常量对象的引用,这个常量对象是一个标准库类型type_info(类/类类型)
#include <iostream>
using namespace std;
class Human
{
public:
Human();
Human(int);
virtual ~Human();
public:
int m_Age;
char m_Name[100];
public:
virtual void eat() =0;
};
Human::Human()
{
cout << "Human::Human()" << endl;
}
Human::~Human()
{
cout << "Human::~Human()" << endl;
}
Human::Human(int tmp)
{
cout << "Human::Human(int tmp)" << endl;
}
//Men是Human的子类
class Men:public Human
{
public:
Men();
Men(int);
~Men();
private:
public:
virtual void eat();
};
Men::Men()
{
cout << "Men::Men()" << endl;
}
Men::~Men()
{
cout << "Men::~Men()" << endl;
}
Men::Men(int tmp)
{
cout << "Men::Men(int tmp)" << endl;
}
void Men::eat()
{
cout << "men eat mi" << endl;
}
//Women是Human的子类
class Women :public Human
{
public:
Women();
Women(int);
~Women();
private:
public:
virtual void eat();
};
Women::Women()
{
cout << "Women::Women()" << endl;
}
Women::~Women()
{
cout << "Women::~Women()" << endl;
}
Women::Women(int tmp)
{
cout << "Women::Women(int tmp)" << endl;
}
void Women::eat()
{
cout << "women eat miantiao" << endl;
}
int main()
{
//name
Human* phuman = new Men;
const type_info& tp = typeid(*phuman);
cout << tp.name() << endl;
//== !=
return 0;
}
int main()
{
Human* phuman = new Men;
const type_info& tp = typeid(*phuman);
//phuman对象里有一个指针,指向这个对象所在的类Men里的虚函数表
return 0;
}
标签:rtu 编译器 ESS 运算 vat code dba 静态 ide
原文地址:https://www.cnblogs.com/Trevo/p/13343848.html