您的位置:首页 > 编程语言 > C语言/C++

C++判断、比较类型的几种方法

2013-06-06 09:50 337 查看
C++里有很多特性让我们可以判断一个变量或值是什么类型,比较两个类型是不是一样,或者比较两个变量是不是同一类型的。这对学习过程是很有用的。

判断单个变量或值的类型

由于数组和指针复杂性,很多初学者感到无所适从,面对一大堆的东西,不知道到底结果是什么。当然了,首先要知道它的类型。

最简单、实用的方法:利用编译器。如果你故意将一个你不知道的东西,赋给一个毫不相干的变量,那么,编译器会绝对忠实地告诉你“XX类型不能转换成XXX类型”,这样,你就知道它的类型了。例如:

int main ()

int (*apa[10])[10];

double tmp;

tmp = apa[0];

}

这在Dev-C++4.9.8.0(GCC3.2)中,编译后会报错“cannot convert `int (*)[10]' to `double' in assignment”。这样,你就知道apa[0]的类型是int(*)[10]了^__^

............................................................

第二种方法是利用重载。假设,你知道一个变量只能是某几种类型,那么可以把那几种情况都重载了,于是编译器会自动地选择合适的函数,这样也就区分开了,你就知道它是什么类型了。例如:

#include <iostream>

#include <cstdlib>

using namespace std;

void type (int) { cout << "int" << endl; }

void type (int*) { cout << "int*" << endl; }

int main ()

{

int a[10];

type (a+1);

system ("Pause");

return 0;

}

结果会输出“int*”。这个办法很不错。但是如果发生“意外”,它是意料之外的类型,那么你就没有办法了,编译会出错。这时就可以用第一种方法来查看了。

............................................................

第三种方法是用template(模板)来实现。首先定义一个模板函数,对所有类型都说是“未知”类型,然后,再一个一个特化出可能的类型。

#include <iostream>

#include <cstdlib>

using namespace std;

template <typename T>

void type (T) {

cout << "Unknown" << endl;

}

template <>

void type <int> (int) {

cout << "int" << endl;

}

template <>

void type <int*> (int*) {

cout << "int*" << endl;

}

int main ()

{

int a[10];

type (*a+1);

system ("Pause");

return 0;

}

使用template的好处是,对于意料之外的类型,可以输出“Unknown”来表示,不会编译通不过。而且,使用template可以进一步使用更“华丽”的技巧。

............................................................

另外一种方法是typeinfo,这个我还不太熟悉。只简略说一下吧。

#include <iostream>

#include <cstdlib>

#include <typeinfo>

using namespace std;

int main ()

{

cout << "char:" << typeid(char).name() << endl;

cout << "unsigned char:" << typeid(unsigned char).name() << endl;

cout << "signed char:" << typeid(signed char).name() << endl;

system ("Pause");

return 0;

}

你能看到几个字符串,但是你不知道是什么意思。

比较两种类型

比较两种类型方法也不少,可以用template和typeinfo。

用template的话,首先实现直接比较两种类型:

template <typename T, typename U>

struct SameType {

static const bool isSame = false;

};

template <typename T>

struct SameType<T, T> {

static const bool isSame = true;

};

使用要用两种类型,象这样:

int main ()

{

cout << "unsigned and unsigned int is: "

<< SameType <unsigned, unsigned int>::isSame << endl;

cout << "long and int is: "

<< SameType <long, int>::isSame << endl;

system ("Pause");

return 0;

}

............................................................

第二种可以比较变量或值,用函数封装来实现:

template <typename T, typename U>

struct SameType {

static const bool isSame = false;

};

template <typename T>

struct SameType<T, T> {

static const bool isSame = true;

};

template <typename T, typename U>

bool isSameType (T, U) {

return SameType <T, U>::isSame;

}

使用时可以用两个变量,象这样:

int main ()

{

int a, b;

long c;

cout << "int a and int b:" << isSameType (a, b) << endl;

cout << "long c and int a:" << isSameType (c, a) << endl;

system ("Pause");

return 0;

}

............................................................

用typeinfo实现:

template <typename T, typename U>

bool isSameType (T t, U u) {

return typeid(t) == typeid(u);

}

使用时象这样:

int main ()

{

int a, b;

long c;

cout << "int a and int b:" << isSameType (a, b) << endl;

cout << "long c and int a:" << isSameType (c, a) << endl;

system ("Pause");

return 0;

}

我的这些方法只能比较两个类型是不是一样,不能得到其它更详细的信息了。要想得到更强大的功能,请看《Modern C++ Design》。

............................................................

好了,方法很多,应该有用吧。

http://bbs.csdn.net/topics/30334109
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: