Resolve scope with operator overloading

I figured out how to get operator overloads to work, but how can I define the (overloaded)functions outside the class definition using the scope resolution operator"::". Here is what I am trying to do:

class foo {
  public:
    foo() : _val(0) {}

    foo & operator =(uint8_t);
    operator uint8_t(); // << having trouble on this one

  private:
    uint8_t _val;
};

// got this one working after sometime:
foo & foo::operator = (uint8_t newVal)
{
  _val = newVal;
}

// how do I get this one working!?:
uint8_t foo::operator()
{
  return _val;
}

void setup()
{
  Serial.begin(115200);

  foo foo_1;
  Serial.println(foo_1); // << expect a 0
  foo_1 = 4;
  Serial.println(foo_1); // << expect a 4
}

void loop() {}

I want to know the correct way to use operator overloads with the scope resolution operator

one other small question:
what is the difference between this:
foo & operator =(uint8_t);
and this? :
foo operator =(uint8_t);
they both seem to work

Can someone please explain what are these 2 lines are intended to do ?

1 Like
foo & operator =(uint8_t);  //  sets _val
operator uint8_t();  //  returns _val

Is that intended to be an explanation ?

sets _val of what ?
returns _val of what ?

1 Like

āžœ seems you are mixing different things (assignment versus copy constructor)

For converting a Foo instance to a uint8_t you need indeed to implement a type-cast operator

try this (typed here, fully untested)

class Foo {
  public:
    Foo() : _val(42) {}
    void operator=(const uint8_t &);
    operator uint8_t() const ;

  private:
    uint8_t _val;
};

void Foo::operator=(const uint8_t &in) {
  _val = in;
}

Foo::operator uint8_t() const {
  return _val;
}


void setup()
{
  Serial.begin(115200);
  Foo foo;
  Serial.println(foo); // << expect a 42
  foo = 4;
  Serial.println(foo); // << expect a 4
}

void loop() {}
1 Like

it set "_val" - foo::_val;
it returns "_val" - foo::_val;

I don't think I am understanding your question

yes, this does work.

wow this stuff gets complicated fast

Thank you Very much :slightly_smiling_face:, especially for the link " assignment versus copy constructor"

Your explanation was not very clear. @UKHeliBob was asking for a more verbose explanation of your intent. Seems I guessed right, glad it helped.

I feel like I confused myself more with the question. I don't even know how to word it correctly. It is very late and I need to sleep, not thinking straight

:slight_smile:
good night !

1 Like

haha, I think you meant to say good morning :sweat_smile:

I think I meant "sleep tight" :slight_smile:

1 Like

It's more conventional to specify a return type for 'operator=()':

class Foo {
  public:
    Foo() : _val(42) {}
    Foo & operator=(const uint8_t &);
    operator uint8_t() const ;

  private:
    uint8_t _val;
};

Foo & Foo::operator=(const uint8_t &in) {
  _val = in;
  return *this;
}

Foo::operator uint8_t() const {
  return _val;
}


void setup()
{
  Serial.begin(115200);
  delay(1000);
  Foo foo;
  Serial.println(foo); // << expect a 42
  foo = 4;
  Serial.println(foo); // << expect a 4

  Serial.println(foo = 100); // << expect 100
}

void loop() {}

you are correct indeed

and then may be add a constructor from an int

class Foo {
  public:
    Foo() : _val(42) {}
    Foo(const uint8_t &in) : _val(in) {}
    Foo & operator=(const uint8_t &);
    operator uint8_t() const ;

  private:
    uint8_t _val;
};

Foo & Foo::operator=(const uint8_t &in) {
  _val = in;
  return *this;
}

Foo::operator uint8_t() const {
  return _val;
}


void setup()
{
  Serial.begin(115200);
  delay(1000);
  Foo foo = 35;
  Serial.println(foo); // << expect a 35 from constructor

  foo = 4;
  Serial.println(foo); // << expect a 4 from assignment 

  Serial.println(foo = 100); // << expect 100 from assignment
}

void loop() {}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.