关于C++的强类型传换!!!!
比如atoi函数将字符串"123" 返回一个int 123;
#include <iostream>
using namespace std;
int atoi(const char*);
int main()
{
char* a = "1234";
int c = atoi(a);
cout << c << endl;
system("pause");
}
int atoi(const char* a)
{
int c = reinterpret_cast<int>(a);
return c;
}
可是不行呀。 reinterpret_cast返回的一定是指针吗?我的书上写的不明不白的。
问题点数:10、回复次数:4Top
1 楼Andy84920(你也不懂)回复于 2004-08-02 23:00:18 得分 5
reinterpret_cast通常对于操作数的"位模式"执行一个比较低层次
的重新解释.
Top
2 楼UPCC(杂食动物)回复于 2004-08-02 23:02:14 得分 5
atoi首先在标准库定义了,你在这里定义会编译不通过的。
而且int c = reinterpret_cast<int>(a);c获得的是a的第一个字符的地址,因为地址的位数和指针一样大所以没事,你可以这样看看
cout << (char*)c << endl;
输出为1234Top
3 楼lbsjs(纪舒)回复于 2004-08-03 00:15:56 得分 0
比如atoi函数将字符串"123" 返回一个int 123;
谁知道这样的函数怎么实现吗??
Top
4 楼luomingmao129(小猫)回复于 2004-08-03 12:59:38 得分 0
你的意思是将一个字符串变成一个整形常量,可是可以,不过好象要将"123"这样的字符串变成整形123应该不可能吧!Top




