Chinaunix首页 | 论坛 | 博客
  • 博客访问: 27638
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 102
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-11 15:40
文章分类
文章存档

2014年(7)

2013年(1)

我的朋友

分类: C/C++

2014-02-08 11:05:54

在此处仅以一个例子做说明:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <string>
  5. #include <iostream>

  6. using namespace std;

  7. class Number
  8. {
  9. public:
  10.     string type;
  11.  
  12.     Number(): type("void") { }
  13.     explicit Number(short) : type("short"){}
  14.     Number(char) : type("char") { }
  15.     Number(int) : type("int") { }
  16. };

  17. void Show(const Number& n)
  18. {
  19.     cout << n.type << endl;
  20. }

  21. void f()
  22. {
  23.     short s = 42;
  24.     Show (s);   

  25.     char b = 'b';
  26.     Show (b);

  27.     Number a(s);
  28.     Show(a);
  29. }

  30. int main()
  31. {
  32.     f();

  33.     return 0;
  34. }
输出为:
int
char
short

总结:explicit   只对构造函数起作用,用来抑制隐式转换。
具体可参考下面链接:
http://www.cnblogs.com/cutepig/archive/2009/01/14/1375917.html
阅读(928) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~