在此处仅以一个例子做说明:
-
#include <stdio.h>
-
#include <string.h>
-
#include <stdlib.h>
-
#include <string>
-
#include <iostream>
-
-
using namespace std;
-
-
class Number
-
{
-
public:
-
string type;
-
-
Number(): type("void") { }
-
explicit Number(short) : type("short"){}
-
Number(char) : type("char") { }
-
Number(int) : type("int") { }
-
};
-
-
void Show(const Number& n)
-
{
-
cout << n.type << endl;
-
}
-
-
void f()
-
{
-
short s = 42;
-
Show (s);
-
-
char b = 'b';
-
Show (b);
-
-
Number a(s);
-
Show(a);
-
}
-
-
int main()
-
{
-
f();
-
-
return 0;
-
}
输出为:
int
char
short
总结:explicit 只对构造函数起作用,用来抑制隐式转换。
具体可参考下面链接:
http://www.cnblogs.com/cutepig/archive/2009/01/14/1375917.html
阅读(928) | 评论(0) | 转发(0) |