Hi i'm trying to overload the assignment operator of a class to return an class Member (data).
class A{
public:
int32_t ToInt32() { return this->data; }
void SetData(int32_t data) { this->data=data; }
private:
int32_t data;
}
I want to overload the = operator, so that i can do the following:
A *a = new A();
a->SetData(10);
int32_t Testint;
Testint = a;
and now a should be 10.
How can i do that?
You can’t do that since
ais a pointer. You can only overload operators for custom types (and pointers are not custom types).But using a pointer here is nonsense anyway. You can make the following work:
In order to do this, you overload the implicit-conversion-to-
int32_toperator, notoperator=:A word on code style: having setters is usually a very bad idea – initialise your class in the constructor. And don’t declare variables without initialising them. So your code should actually look like this:
… two lines instead of four. The definition of
Abecomes simpler as well: