我想超载" =" operator将值从对象赋值给非类int变量

时间:2014-06-10 12:18:33

标签: c++ overloading assignment-operator

int i;
Integer a; //a class object

i=a; //here a's member variable value should be assigned to 'i'

1 个答案:

答案 0 :(得分:5)

您可以通过提供从班级到int的转化运算符来实现此目的:

class Integer
{
 // ... stuff
public:
  operator int() const
  { return member_to_assign; }
};