| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1700 人关注过本帖
标题:请教关于assert和explicit
只看楼主 加入收藏
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
结帖率:66.67%
收藏
 问题点数:0 回复次数:4 
请教关于assert和explicit

最近看到头文件assert.h和关键字explicit,前者是针对出错吧?它注释说当遇到逻辑上的错误就终止,到底怎么终止?关键字的注释小弟没看懂,能举个例子吗?

class vect { public: explicit vect(int i=0); //不转换类型从int到vect,什么意思难道其它构造函数都转换吗? ...};

搜索更多相关主题的帖子: assert explicit 
2004-09-07 10:54
young
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:223
专家分:160
注 册:2004-9-5
收藏
得分:0 
小弟刚学C++不久,我要说一下assert
assert是断言, 是帮助开发者查找错误的.一般在DEBUG是使用.是一个宏.
如:
 assert(p != NULL);
假设指针p不是指向NULL的话(即为true),程序什么事都没有,继续跑
但是如果括弧里面的值为false的话, 就会跳出错误窗口,并且会写明在哪行错了,然后各位大哥可以用Ctrl + g输入
行数,立刻找到.哈哈.
断言大多在函数的入口处和出口出设置,这是一种很好的检测错误的方法.
不过比较麻烦,不如直接使用断点加quick watch,然后单步执行的方法来得方便
explicit小弟见过,但是没用过,所以不敢在这吹,请那位高手来详细说明一下.

如果你爱C语言,请你爱指针; 如果你爱指针,请你爱指针的指针;
2004-09-07 19:51
kuangjingbo
Rank: 1
等 级:新手上路
帖 子:312
专家分:0
注 册:2004-4-24
收藏
得分:0 

楼上说的基本正确

关于explicit,我归纳不好,我从msdn上给你摘了,看看他们怎么说

先贴英语,我再给你翻译成汉语

explicit C++ Specific

This keyword is a declaration specifier that can only be applied to in-class constructor declarations. Constructors declared explicit will not be considered for implicit conversions. For example:

class X { public: explicit X(int); //legal explicit X(double) { //legal // ... } };

explicit X::X(int) {} //illegal An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object. For

example, with the class declared above:

void f(X) {} void g(int I) { f(i); // will cause error } void h() { X x1(1); // legal } The function call f(i) fails because there is no available implicit conversion from int to X.

Note It is meaningless to apply explicit to constructors with multiple arguments, since such constructors cannot take part in implicit conversions.


永不放弃!
2004-09-07 20:39
kuangjingbo
Rank: 1
等 级:新手上路
帖 子:312
专家分:0
注 册:2004-4-24
收藏
得分:0 

我的翻译:

explicit C++ 约定

此关键字是一个声明约定,只能用于在类内的构造函数的声明。声明了explicit的构造函数

不能用于隐式转换。比如:

class X { public: explicit X(int); //合法 explicit X(double) { //合法 // ... } };

explicit X::X(int) {} //非法,因为声明用在类外了

一个explicit构造函数不能用于隐式转换.它只能用于显式的构造一个对象.比如,用上面的

类的声明:

void f(X) {} void g(int I) { f(i); // 将引起错误 } void h() { X x1(1); // 合法 } 函数调用 f(i)失败,因为没有从int型到X型的隐式转化。

注意: 把explicit应用于多参数构造函数是没有意义的, 因为这样的构造函数不能用于隐

式转换。


永不放弃!
2004-09-07 20:54
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

谢谢两位,前两天没空上来。

2004-09-09 21:47
快速回复:请教关于assert和explicit
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014695 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved