#include <iostream>
using namespace std;
class A
{
private:
float data_member;
public:
A(int a);
explicit A(float d);
};
A::A(int a)
{
data_member = a;
}
A::A(float d)
{
data_member = d;
}
void Test(A a)
{
cout<<"Do nothing"<<endl;
}
int main()
{
Test(12);
Test(12.6); //Expecting a compile time error here
return 0;
}
我期待錯誤int這種情況下,因爲我的CTOR採取浮動值是顯式的。但是我在VS 2010中沒有收到任何錯誤。請指出我是否對我對C++中關鍵字「EXPLICIT」的理解有誤。爲什麼我在下面的代碼中沒有出錯?
瞭解明確的關鍵字的用法如下: http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean – 2013-05-03 05:45:31
@AayushiJain的評論謝謝,但我已經閱讀了這篇文章。 – 2013-05-03 05:56:59