为了清楚起见,让我的新课程成为:
class MyInt{
public:
MyInt(int x){theInt = x /10;}
int operator+(int x){return 10 * theInt + x;}
private
int theInt;
};
Run Code Online (Sandbox Code Playgroud)
让我们说我希望能够定义:
MyInt Three(30);
int thirty = Three;
Run Code Online (Sandbox Code Playgroud)
但是为了得到这个结果,我写道:
MyInt Three(30);
int thirty = Three + 0;
Run Code Online (Sandbox Code Playgroud)
如何从我的Custom类自动转换为内置类型?
jro*_*rok 14
具有类型转换功能:
class MyInt{
public:
MyInt(int x){theInt = x /10;}
int operator+(int x){return 10 * theInt + x;}
operator int() const { return theInt; } // <--
private
int theInt;
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4221 次 |
| 最近记录: |