本文共 718 字,大约阅读时间需要 2 分钟。
轉載自
The type-cast operator uses a particular syntax: it uses the operator
keyword followed by the destination type and an empty set of parentheses.
Notice that the return type is the destination type and thus is not specified before the operator
keyword.
// implicit conversion of classes:#includeusing namespace std;class A {};class B {public: // conversion from A (constructor): B (const A& x) {} // conversion from A (assignment): B& operator= (const A& x) { return *this;} // conversion to A (type-cast operator) operator A() { return A();}};int main (){ A foo; B bar = foo; // calls constructor bar = foo; // calls assignment foo = bar; // calls type-cast operator return 0;}
转载地址:http://brsgi.baihongyu.com/