Operator Overloading

Hi All.

So, I understand the basics of simple operator overloading. For example, the assignment operator:

class myClass {
  private:
    uint32_t privateValue;

  public:
    myClass & operator=(uint32_t);
};

myClass & myClass::operator=(uint32_t newValue) {
  privateValue = newValue;
  return *this;
}

myClass myObject;

void setup() {
  myObject = 100;
}

But, what if I want to do the reverse. In this example, get the object’s ‘privateValue’ and assign it to a uint32_t:

uint32_t x;
x = myObject;

Is there a way to over load the assignment operator in the “other direction”? Or, do I need to implement a getPrivateValue() method?

Thanks.

Just have more than one overload!

Mark

So what would be the syntax of the overload declaration allowing this?

uint32_t x;
x = myObject;

Seems like it would required overloading the assignment operator of the uint32_t type.

uint32_t x;
x = myObject; // <<<< this is the assignment operator for type uint32_t

BulldogLowell:
<<<< this is the assignment operator for type uint32_t

Understood, and that’s my conundrum. But, I can overload the [] operator to “sort of” do what I’m asking:

class myClass {
  private:
    uint32_t privateValue;

  public:
    myClass & operator=(uint32_t);
    uint32_t operator[](uint8_t);
};

myClass & myClass::operator=(uint32_t newValue) {
  privateValue = newValue;
  return *this;
}
uint32_t myClass::operator[](uint8_t) {
  return privateValue;
}

myClass myObject;

void setup() {
  uint32_t x;
  Serial.begin(115200);
  delay(1000);
  myObject = 100;
  x = myObject[0];
  Serial.println(x);
}

void loop() {}

But, this seems cheesy and ugly since it requires adding [index] to every access.

So, the only other choice is a getPrivateValue() method?

uint32_t myClass::getPrivateValue() {
  return privateValue;
}

So, the only other choice is a getPrivateValue() method?

What's wrong with that? It makes it quite clear that you are not trying to assign an object to a uint32_t variable.

gfvalvo:
But, this seems cheesy and ugly since it requires adding [index] to every access.

So, the only other choice is a getPrivateValue() method?

uint32_t myClass::getPrivateValue() {

return privateValue;
}

your example is a bit strange to me, a pseudo-code analogy:

Class Dog{
  private:
    Leg _leg;
};
...
...

Dog dog;
Leg front;
front = dog;

... corrected my code

a pseudo-code analogy:

I think that what OP is trying to do is have a uint32_t member, legCount, and a variable, numberOfLegs, and be able to say:

   Dog mutt;
   mutt.SetLegCount(8);

   uint32_t numberOfLegs = mutt;

But I count figure out why, when

   uint32_t numberOfLegs = mutt.GetLegCount();

makes it much clearer what is being assigned.

PaulS:
What's wrong with that?

Nothing wrong with it, I’m just trying to grow my understanding of the language.

Also, a sense of symmetry says that if I can overload things to allow:

myObject = x;

Then I should also be able to overload to get:

x = myObject;

BulldogLowell:
your example is a bit strange to me

Let’s say myClass represents an RGB LED that has lots of methods (dimming, flashing, etc) and many internal properties (RBG color, flash rate, etc).

So, I can overload operator= to set the color for example:

uint32_t rgbColor = 0x00FF2310;
myObject = rgbColor;

So, from symmetry, maybe I’d like to do:

rgbColor = myObject;

Provide a conversion function in your class like:

operator int() const { return mint; }

Perfectly reasonable to do when used judiciously.

arduarn:
Provide a conversion function in your class like:

operator int() const { return mint; }

Perfectly reasonable to do when used judiciously.

Thanks. That's the syntax I was looking for. And, I learned something about the language. The term I was groping for is "user-defined conversion".

Unfortunately, this site: User-defined conversion function - cppreference.com

is long on formalism and short on practical usage advice.