#Understanding why declaring operators has so much repetition

66 messages · Page 1 of 1 (latest)

empty bronzeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

idle edge
#

Additionally, what do the first and last const accomplish? Why do they need to be repeated?

tidal ocean
#

because overloading operators is not limited to integers, and the types can be different, depending on what you want to do

#

you may want to have a string type that repeats itself with operator * for example

#

one of repetitions you shown is due to the operator being a member function of class Integer

pulsar locust
idle edge
lethal finch
#

Yea

idle edge
lethal finch
#

It is

tidal ocean
#

I wasn't talking about constness

lethal finch
#

But inside the class you don’t need Integer::

idle edge
#

I think I asked one too many questions at once 😅

tidal ocean
#

in this particular code, only a single const is necessary, the rest shouldn't be there

boreal oxide
#

this should not be returning a const value.

tidal ocean
#

or actually 2

boreal oxide
#

however, it should be a free function, accepting two const& params, to allow either side to be convertible

idle edge
#

Lemme illustrate my confusions, one sec

tidal ocean
#

;compile cpp

struct Integer {
  int i = 0;
  Integer(int val) : i(val) { }
};

Integer operator+(Integer left, Integer right) {
  return Integer(left.i + right.i);
}

int main() {
  Integer(50) + 10;
}
thorn echoBOT
#
Compilation successful

No output.

idle edge
#

For example, which Integer is being referenced in the black line?

boreal oxide
idle edge
#

Huh

idle edge
#

Then

#

What is being referenced on the Integer before the black line?

#

Also green?

lethal finch
#

Red

tidal ocean
#

in this case it would be even slower to pass it by const& if it wasn't inlined

idle edge
#

Ah that explains my confusion

#

I though it was backwards since the red one was nested inside the green one

#

Meaning the final result should look like this?

tidal ocean
#

yes

idle edge
#

Alright that's the first confusion out of the way! cat_up

#

Now for the three consts

tidal ocean
#

IDK where do you learn it from, but they teach you wrong practices

#

void main() is invalid C++

idle edge
#

Yeah, it was annoying when I was copy pasting code

#

We're using [checks]

tidal ocean
#

the code should be implemented like this:

#

;compile cpp

#include <iostream>

struct Integer {
  int value = 0;
};

void print(Integer i) {
  std::cout << i.value << '\n';
}

Integer operator+(Integer left, Integer right) {
  return Integer{left.value + right.value};
}

int main() {
  print(Integer{10} + Integer{20});
}
thorn echoBOT
#
Program Output
30
tidal ocean
#

free functions where appropriate and no const overuse

idle edge
#

So for the consts, what I understand is according to their order is:
First: Don't try to modify the function per se other than through my own code
Second: Do not modify the input value whatsoever (right side)
Third: Don't modify the right and left side values, but I think this is covered by the second one?

#

Lemme copy paste that to compare it side by side

lethal finch
#

First is “this function returns a non modifiable object”

#

Third is “this function won’t change the state of the class it belongs to”

idle edge
#

What does 'state' here mean?

tidal ocean
#

held data

lethal finch
#

int i inside Integer

#

In this case

idle edge
#

Another case solved by the team

idle edge
#

I'll look over your code and try to learn from it Paweł

#

!solved

empty bronzeBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

idle edge
#

The image so that you guys don't have to scroll around

#

Oh wait I see what I got wrong

#

!solved