-5

I want to overload the =operator. It should work like that:

MyClass a;
double b=a;

How can I do that?

Tschüss, Andre

Andre
  • 1,179
  • 1
  • 12
  • 34

1 Answers1

2

That's not operator= (assignment). It's an initialiser. You would normally do something like this by providing a constructor that takes MyClass as an argument - however, you can't do that for double. Instead, you need to provide a conversion function for MyClass:

class MyClass
{
  public:
    operator double() const { return 5.0; }
};
Joseph Mansfield
  • 104,685
  • 19
  • 232
  • 315