#include<iostream>
using namespace std;
class X
{
public:
operator int()
{ return 0; }
} x;
class Y
{
public:
operator X()
{
return x;
}
} y;
int main()
{
//int a = y; /* error Y没有定义该类转换方式 */
int b = X(y); /* ok */
// int c = y++; /* error Y没有重载++ */
// int c = X(y)++;
// int e = int(X(y))++; /* error 转换后是右值 */
return 0;
}
C++可能通过operator 重载隐式转换,格式如下:
operator 类型T ()
class A
{
public:
operator B* () { return this->b_;}
operator const B* () {return this->b_;}
operator B& () {return *this->b_;}
private: B* b_;
};