typeid, const_cast<Type>的使用
2024-10-08 00:31:08
#include <bits/stdc++.h>
using namespace std; class A {
public :
void Show() {
cout << "A" << endl;
}
} ; class CC {
public :
void Show() {
cout << "CC" << endl;
}
} ; class x {
public :
void Show() {
cout << "x" << endl;
}
} ; class {
} test; int main() {
long long a, *b = ;
int *c;
double *d;
cout << (typeid(a) == typeid(long long)) << endl;
cout << (typeid(a) == typeid(double)) << endl;
b = &a;
cout << (typeid(*b) == typeid(long long)) << endl;
cout << typeid(b).name() << endl;
cout << typeid(*b).name() << endl;
cout << typeid(c).name() << endl;
cout << typeid(*c).name() << endl;
cout << typeid(d).name() << endl;
cout << typeid(*d).name() << endl;
A t;
cout << typeid(t).name() << endl;
CC s;
cout << typeid(s).name() << endl;
x *p;
cout << typeid(p).name() << ' ' << typeid(*p).name() << endl;
class {
} temp;
cout << typeid(temp).name() << endl;
cout << typeid(test).name() << endl;
class {
} temp2;
cout << typeid(temp2).name() << endl;
class a {
} temp3;
cout << typeid(temp3).name() << endl;
cout << typeid(long).name() << endl;
unsigned long long l;
cout << typeid(l).name() << endl;
short z;
cout << typeid(z).name() << endl;
return ;
}
运行结果:
Px
x
Pi
i
Pd
d
1A
2CC
P1x 1x
Z4mainEUt_
._125
Z4mainEUt0_
Z4mainE1a
l
y
s // 在ubuntu 13.10下的运行结果
const_cast<Type>去除const或volatile限制:
#include <bits/stdc++.h>
using namespace std; char *Test(const char *s) {
char *t = const_cast<char *>(s);
*t = 'z';
return t;
} int main() {
const char *s1 = "abc";
const char s2[] = "cba";
//cout << Test(s1) << endl; // Run-Time Error
cout << Test(s2) << endl;
cout << s2 << endl;
//cout << Test("ABC") << endl; // Run-Time Error
return ;
}
#include <bits/stdc++.h>
using namespace std; void change(const int *pt) {
int *pc = const_cast<int *> (pt);
*pc = ;
} int main() {
int pop1 = ;
const int pop2 = ;
cout << pop1 << ' ' << pop2 << endl;
change(&pop1);
change(&pop2);
cout << pop1 << ' ' << pop2 << endl;
return ;
}
——written by Lyon
最新文章
- 如何发布带静态资源的库——android 篇
- 【Prince2是什么】PRINCE2认证之Prince2衡量绩效的六大要素
- 自定义刻度的SeekBar
- SQL Server 2012 安装
- kmp
- js 小工具-- 原生 js 去除空格
- OpenGL2.0及以上版本中glm,glut,glew,glfw,mesa等部件的关系
- ldd查询命令或软件共享的函数库(动态)
- ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java
- BZOJ 2120/BZOJ 2453
- Jquery 回到顶部
- Android Studio 设置LogCat 颜色
- 【HDU 4451 Dressing】水题,组合数
- 判图的连通性(dfs,并查集)
- Oracle常用函数:DateDiff() 返回两个日期之间的时间间隔自定义函数
- Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.FilterDefinition
- 第五十五天 css基础入门
- Sqlite之事务
- C#,如何程序使用正则表达式如何使用匹配的位置的结果修改匹配到的值
- 开始学习Scheme