更新时间:2023-11-10 08:23:22
您可以实现类型转换运算符来实现这一点,例如
class A
{
int m_a;
public :
A( int a = 0 ):m_a(a){}
operator int (){ return m_a;} // 类型转换操作符到'int'类型
};
int _tmain( int argc,_TCHAR * argv [])
{
A obA;
int a = obA;
return 0 ;
}
how to make it work the line int a = obA; in the following code snippet.
class A
{
int m_a;
public:
int operator =(int a)
{
m_a = a;
return 0;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A obA;
int a = obA;
return 0;
}
You might implement the typecast operator to make that work, e.g.class A { int m_a; public: A(int a=0):m_a(a){} operator int(){ return m_a;} // typecast operator to 'int' type }; int _tmain(int argc, _TCHAR* argv[]) { A obA; int a = obA; return 0; }