返回   cpper编程论坛 > C/CPP/TMP/GP
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2007-06-04
polyrandom 的头像
超级版主
 
注册日期: 2002-09-03
帖子: 3,135
文章: 20
polyrandom 正向着好的方向发展
默认 bool到void*的自动转换

C++总是让我吃惊,今天我写了一段程序,每次运行都会crash。可以归结为下面这样的代码:
代码:
class Test { public: Test( const void* p ) {} }; void func( const Test& ) { } int main() { func( false ); }
这样的代码居然是可以编译通过的,而func( true );却不能。
看来C++的确总会给我带来一些惊喜
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2007-06-04
bankrock 的头像
高级会员
 
注册日期: 2003-12-11
帖子: 843
文章: 7
bankrock 正向着好的方向发展
默认 回复: bool到void*的自动转换

有点奇怪,我记得标准转换序列只能有一个promotion或者一个standard conversion,这里似乎是bool>int->void*,有一个promotion一个standard conversion,照理应该不行。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2007-06-04
高级会员
 
注册日期: 2002-09-16
帖子: 1,087
文章: 1
SpitFire 正向着好的方向发展
默认 回复: bool到void*的自动转换

0总是特殊的

over
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #4 (permalink)  
旧 2007-06-05
高级会员
 
注册日期: 2002-09-19
帖子: 839
文章: 7
tomato 正向着好的方向发展
默认 回复: bool到void*的自动转换

原先遇到过同样的问题
记录一个号码
原本函数参数是int,但是发现int可能不够用,于是改成了char*
但是函数调用处的参数没有由0改为"0"
。。。

后来重载一个int版本并且access为private就可以在编译时期找出这种问题了
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #5 (permalink)  
旧 2007-06-05
高级会员
 
注册日期: 2002-09-19
帖子: 839
文章: 7
tomato 正向着好的方向发展
默认 回复: bool到void*的自动转换

引用:
作者: bankrock 查看帖子
有点奇怪,我记得标准转换序列只能有一个promotion或者一个standard conversion,这里似乎是bool>int->void*,有一个promotion一个standard conversion,照理应该不行。
实际上false -> (void*)0
引用:
4.10 1
A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to
zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that
type and is distinguishable from every other value of pointer to object or pointer to function type.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #6 (permalink)  
旧 2007-06-05
polyrandom 的头像
超级版主
 
注册日期: 2002-09-03
帖子: 3,135
文章: 20
polyrandom 正向着好的方向发展
默认 回复: bool到void*的自动转换

引用:
作者: tomato 查看帖子
实际上false -> (void*)0
问题是,这不是一个#define FALSE (0),如果false也被map到0,我觉得很恶心的。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #7 (permalink)  
旧 2007-06-05
bankrock 的头像
高级会员
 
注册日期: 2003-12-11
帖子: 843
文章: 7
bankrock 正向着好的方向发展
默认 回复: bool到void*的自动转换

引用:
A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to
zero.
bool是integer type?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #8 (permalink)  
旧 2007-06-05
初级会员
 
注册日期: 2007-04-03
帖子: 14
zigzed 正向着好的方向发展
默认 回复: bool到void*的自动转换

引用:
作者: polyrandom 查看帖子
C++总是让我吃惊,今天我写了一段程序,每次运行都会crash。可以归结为下面这样的代码:
代码:
class Test { public: Test( const void* p ) {} }; void func( const Test& ) { } int main() { func( false ); }
这样的代码居然是可以编译通过的,而func( true );却不能。
看来C++的确总会给我带来一些惊喜
一般单参数的构造函数我习惯加上explicit。所以没有遇到过类似的问题。
不过还是让我吃惊。。。。。。。这是C++引以为豪的静态类型系统上的弹孔?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #9 (permalink)  
旧 2007-06-05
高级会员
 
注册日期: 2002-09-16
帖子: 1,087
文章: 1
SpitFire 正向着好的方向发展
默认 回复: bool到void*的自动转换

我已经非常非常习惯的加explicit

over
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #10 (permalink)  
旧 2007-06-05
polyrandom 的头像
超级版主
 
注册日期: 2002-09-03
帖子: 3,135
文章: 20
polyrandom 正向着好的方向发展
默认 回复: bool到void*的自动转换

引用:
作者: SpitFire 查看帖子
我已经非常非常习惯的加explicit

over
让我出问题的是std:tring。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #11 (permalink)  
旧 2007-06-06
高级会员
 
注册日期: 2002-09-19
帖子: 839
文章: 7
tomato 正向着好的方向发展
默认 回复: bool到void*的自动转换

引用:
作者: polyrandom 查看帖子
让我出问题的是std:tring。
我觉得重载一个int版本对string够用了,修改一下crt或是libc而已
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #12 (permalink)  
旧 2007-06-06
高级会员
 
注册日期: 2002-09-19
帖子: 839
文章: 7
tomato 正向着好的方向发展
默认 回复: bool到void*的自动转换

引用:
作者: bankrock 查看帖子
bool是integer type?
3.9.1 7
引用:
Types bool, char, wchar_t, and the signed and unsigned integer types are collectively called integral
types.43) A synonym for integral type is integer type. The representations of integral types shall define values
by use of a pure binary numeration system.44)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #13 (permalink)  
旧 2007-06-06
高级会员
 
注册日期: 2002-09-19
帖子: 839
文章: 7
tomato 正向着好的方向发展
默认 回复: bool到void*的自动转换

引用:
作者: tomato 查看帖子
我觉得重载一个int版本对string够用了,修改一下crt或是libc而已
呵呵,我在原先公司就是这么做的。。。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #14 (permalink)  
旧 2007-06-06
高级会员
 
注册日期: 2002-09-19
帖子: 839
文章: 7
tomato 正向着好的方向发展
默认 回复: bool到void*的自动转换

引用:
作者: polyrandom 查看帖子
问题是,这不是一个#define FALSE (0),如果false也被map到0,我觉得很恶心的。
同意,我觉得null就该用个关键字
gcc就是这么做的
java也有这个关键字
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #15 (permalink)  
旧 2007-06-06
bankrock 的头像
高级会员
 
注册日期: 2003-12-11
帖子: 843
文章: 7
bankrock 正向着好的方向发展
默认 回复: bool到void*的自动转换

败了,这么说bool, char和其他整型都会出这种事真阴险
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #16 (permalink)  
旧 2007-12-30
原来我什么都不会
 
注册日期: 2007-11-14
住址: Bei Jing
帖子: 2
东方晨曦 正向着好的方向发展
发送 MSN 消息给 东方晨曦
默认 回复: bool到void*的自动转换

0 == NULL,
但不能给指针赋个地址。
true, 非0,(地址?)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用



所有时间均为格林尼治时间 +9。现在的时间是 07:21 AM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2008,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0