读书人

How to test if the two data#039;s type

发布时间: 2012-02-19 19:43:37 作者: rapoo

How to test if the two data's type are the same?
Hi,

I got a problem as the title describes.

I have seen someone do as the following

(void)(&a==&b)

so, if the expression is 0, you got a different type.But i am confused with that.

[解决办法]
(void)(&a==&b)
This expression according to operater take an address, whatsoever the data type, compared bit by bit with the memory data by the memory length .

[解决办法]
You can use "typeid() ",a simple function
[解决办法]
char ch1;
char ch2;
int i;
then typeid(ch1)==typeid(ch2),and typeid(i)=\=typeid(ch1)
[解决办法]
#include <typeinfo.h>

const type_info& t1 = typeid( cha1);
const type_info& t2 = typeid( cha2);

bool b = strcmp(t1.raw_name(),t2.raw_name())

读书人网 >C语言

热点推荐