-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,249
  • 1
  • 15
  • 38

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
  • 108,238
  • 20
  • 242
  • 324