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.
66 messages · Page 1 of 1 (latest)
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.
Additionally, what do the first and last const accomplish? Why do they need to be repeated?
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
The first const means the Integer object it returns is const so it cannot be modified by the caller. The last const states that in that function neither rv or the left hand side object you're adding can be modified
Hmm and the middle const would indicate that whatever's being passed can't be modified per se? (like for example jj can't be equal to 3 at the end?)
Yea
Ah, so the const Integer inside the class isn't a 1:1 translation to the const Integer outside the class?
It is
I wasn't talking about constness
But inside the class you don’t need Integer::
I think I asked one too many questions at once 😅
in this particular code, only a single const is necessary, the rest shouldn't be there
this should not be returning a const value.
or actually 2
const& param probably should be allowed to allow for implicit conversions from int
however, it should be a free function, accepting two const& params, to allow either side to be convertible
Lemme illustrate my confusions, one sec
;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;
}
No output.
both are passed by value here
For example, which Integer is being referenced in the black line?
ig for something as trivial as this, there's no cost to copying. but in general, i would prefer to not
Green
Huh
yeah, it depends
Red
in this case it would be even slower to pass it by const& if it wasn't inlined
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?
yes
IDK where do you learn it from, but they teach you wrong practices
void main() is invalid C++
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});
}
30
free functions where appropriate and no const overuse
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
First is “this function returns a non modifiable object”
Third is “this function won’t change the state of the class it belongs to”
What does 'state' here mean?
held data
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
So restarting this thread because I saw this other image, and the way it's being referenced seems to be a bit different than the way I drew the diagram for this image below
The image so that you guys don't have to scroll around
Oh wait I see what I got wrong
!solved