呼叫建構函式進行型別轉換

嘰了咣啷biang發表於2012-07-07


 

#include <iostream>
using namespace std;
class A
{
public:
	A(int x){i=x;}      //把這條語句改成 explicit A(int x){i=x;}關閉這個建構函式的隱式轉換。
	~A(){}
private:
	int i;
};
int main()
{
	A a(15);
	a=58;     //a=58等同於a=A(58);這種特性是隱式型別轉換,a=A(58)是顯式的。
	return 0;
}


 

相關文章