[C++] 纯文本查看 复制代码
#include <iostream>
#include <typeinfo>
using namespace std;
class myclass {
//...
};
int main()
{
int i;
float f;
myclass ob;
cout << "The type of i is: " << typeid(i).name() << endl;
cout << "The type of j is: " << typeid(f).name() << endl;
cout << "The type of ob is: " << typeid(ob).name() << endl;
return 0;
}
[C++] 纯文本查看 复制代码
#include <iostream>
#include <typeinfo>
using namespace std;
class base {
virtual void f() { }; //基类是多态类
//...
};
class derived1 : public base {
//...
};
class derived2 : public base {
//...
};
int main()
{
base *p, baseob; //声明一个基类指针和一个基类对象
derived1 ob1;
derived2 ob2;
p = &baseob;
cout << "p is pointing to an object of type ";
cout << typeid(*p).name() << endl;
p = &ob1;
cout << "p is pointing to an object of type ";
cout << typeid(*p).name() << endl;
p = &ob2;
cout << "p is pointing to an object of type ";
cout << typeid(*p).name() << endl;
return 0;
}
[C++] 纯文本查看 复制代码
#include <iostream>
#include <typeinfo>
using namespace std;
class base {
virtual void f() { }; //基类是多态类
//...
};
class derived1 : public base {
//...
};
class derived2 : public base {
//...
};
//对饮用参数使用 typeid
void WhatType(base &ob)
{
cout << "ob is referencing an object of tpe ";
cout << typeid(ob).name() << endl;
}
int main()
{
int i;
base baseob;
derived1 ob1;
derived2 ob2;
WhatType(baseob);
WhatType(ob1);
WhatType(ob2);
return 0;
}
[C++] 纯文本查看 复制代码
#include "stdafx.h"
#include <iostream>
using namespace std;
template <class T> class myclass {
T a;
public:
myclass(T i) { a = i; }
};
int _tmain(int argc, _TCHAR* argv[])
{
myclass<int> ob1(10), ob2(9);
myclass<double> ob3(7.2);
cout << "Type of ob1 is ";
cout << typeid(ob1).name() << endl;
cout << "Type of ob2 is ";
cout << typeid(ob2).name() << endl;
cout << "Type of ob3 is ";
cout << typeid(ob3).name() << endl;
if (typeid(ob1) == typeid(ob2))
cout << "ob1 and ob2 are the same type\\n";
if (typeid(ob3) == typeid(ob1))
cout << "Error\\n";
else
cout << "ob1 and ob2 are different types.\\n";
return 0;
}
[C++] 纯文本查看 复制代码
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
//figure 是一个模板类
template <class T> class figure {
protected:
T x, y;
public:
figure(T i, T j) {
x = i;
y = j;
}
virtual T area() = 0;
};
template <class T> class triangle : public figure<T> {
public:
triangle(T i, T j) : figure<T>(i, j) {}
T area() {
return x * 0.5 * y;
}
};
template <class T> class rectangle : public figure<T> {
public:
rectangle(T i, T j) : figure<T>(i, j) { }
T area() {
return x * y;
}
};
template <class T> class circle : public figure<T> {
public:
circle(T i, T j=0) : figure<T>(i, j) { }
T area() {
return 3.14 * x * x;
}
};
//创建从类 figure 派生的类的对象
figure<double> *generator()
{
switch( rand() % 3 ) {
case 0:
return new circle<double>(10.0);
case 1:
return new triangle<double>(10.1, 5.3);
case 2:
return new rectangle<double>(4.3, 5.7);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
int main()
{
figure<double> *p;
int i;
int t = 0, c = 0, r = 0;
//生成并统计对象
for (i = 0; i < 10; i++) {
p = generator();
cout << "Object is " << typeid(*p).name(); //运行时类型识别
cout << ". ";
//统计对象
if( typeid(*p) == typeid(triangle<double>)) t++;
if( typeid(*p) == typeid(rectangle<double>)) r++;
if( typeid(*p) == typeid(circle<double>)) c++;
cout << "Area is " << p->area() << endl;
}
cout << endl;
cout << "Objects generated:\\n";
cout << " Triangles: " << t << endl;
cout << " Rectangles: " << r << endl;
cout << " Circles: " << c << endl;
return 0;
}