great explanation Mike. I like the way you explain with refering to cppreference page because we get the idea of how we can refer that as we cant remember all the syntax.
You ended with "thanks for your time folks" to which as a now old looney toons I would answer, no, mister, thank YOU for your amazing effort and superblous education! What you're doing here throughout your charitable human oriented spirit is way more... And gloriously so ... Than state education ever did... Wouldn't it be funny if I was I was in fact a chatgpt bot saying this... Or is it better to say language oriented models or something like that? 😂
Hi! semi-self taught programmer here. I have been expertimenting with C++ for over an year. Since, I started watching this series... The way I have been thinking about C++ is fundamentally changing(Not because of just this video). I am grateful for these videos Thanks Mike!😄
Hi, Good job Mike, I could not understand the error @12:46, postfix operator++ not declared. @12:05 you have declared operator++(). Can you please explain why prefix resolved this. Thanks
I probably should have implemented void operator++() { } for prefix (without any argument). Postfix implementation is supposed to make a copy first, modify the copy, and then return the new copy of the object, whereas prefix (i.e. ++vec3f) should just modify the object -- no need for an argument.
When I had both free and member functions declared, I got a warning saying"... these are ambiguous ...", which makes sense. But unlike your example, running mine resulted in using the free function. That is a bit confusing as I thought member functions trump free functions.
Interesting -- There should be some known order for 'overload resolution' -- see if en.cppreference.com/w/cpp/language/overload_resolution or en.cppreference.com/w/cpp/language/operators has it specified.
@@MikeShah running it on godbolt I still get the free function running. And reading the links and consulting with one of my friends, it does make sense as main is closer to the global scope than the class scope. Anyway, I guess I rather move on and deal with this when/if this comes up in a real world example. Thank you.
@@MindHaunter Interesting, I'll have to look closer at the overload resolution. I might have covered this somewhere as well, but seem to forget the priority off the top of my head. Perhaps it is indeed implementation dependent, though i feel like you'd match the member function first, and then the free function intuitively.
Now that I think about it not exactly because in the cpp example it is returning and in the context of an osstream and in Java just returning string but still helpful 🤣
Compiler knows types of each variable? Could you perhaps restate so I understand better (i.e. Do you mean how does the compiler know "someInt + rhs.x" works)?
operator+(const Vector& rhs) takes in as a parameter the vector. The 'x' is the current instance (this.x) of myVector1 that is being added.@@kirandhegaskar9298
Nice video. Somewhere in the middle of this series you might want to do a lesson on testing in C++ and how TDD can help make things easier? I don't know if there is a lightweight testing framework for C++ or something in std that could be purposed for the job? Thanks for the video, really enjoying the series!
Added your request to the wishlist! In this series I will talk about exceptions and assertions at the least. It may belong in a different series on testing, but at some point I would at least talk about Catch2 framework as well.
Hey, I was just working with examples of this code to practice, and I was wondering if I am supposed to declare an operator overload in .h and implement it in .cpp? I'm having some issues with it so I'm not sure. For example, I used (className operator+(const className& rhs); in .h But I implement the code in .cpp and I'm getting declaration issues when I use the scope operator like other class functions. So should I implement the code in .h or how does that work? Thanks!
Typically the declaration is in the header file and implementation in the .cpp file. (Note: On occasion 'inline' operations are implemented in the header)
It's a good question-- the result of operator+ in my demonstration returns a new object. If you assign it, you probably want the '=' operator overloaded. If you overload '=' you probably also want '==' so you can test for equality. I don't know if there's a general name for this, but in general, if you overload +, you probably want to overload '-', '=', and '==' operators at the minimum :)
Hi Mike, great video. I have tried to overload the insertion operator and could not get it right cause i have marked my field private. I did some research and found out it is rather done with friend . Any explanation why?
@@MikeShah even though the data was public, I had to use friend function to get it working. Hope i'll be able to figure out watching the videos further in the series.
You are referring to postfix increment for ++ (keep in mind their is prefix and postfix). postfix increment (e.g. i++) actually returns a new copy that is incremented, so it does not return a reference (See example here: docs.microsoft.com/en-us/cpp/cpp/increment-and-decrement-operator-overloading-cpp?view=msvc-170). Following a similar thought process, operator+ makes a copy. It's a bit strange, but x += y is different than x + y (See bullet point 5 and 6 here: isocpp.org/wiki/faq/operator-overloading#op-ov-rules)
that `rhs) const {` syntax feels so random and misplaced. Go guess where it relates to if you see it the first time. Imagining people getting in one place and like "Aha, yeah, let's add const syntax between signature and body to indicate... whatever". And in the one of previous video there was something like `Array(const Array &rhs) = delete;`. It feels in this language you need just ignore what you are reading aloud because it doesn't make sense anyway. On the left hand side it's a function signature, on the right - just a keyword and `=` itself doesn't mean equality or assignment. Or maybe that's the strategy - make things completely weird, so everyone LOL about it and like "yeah, that was good one. Better than `rhs) const {` or delete vs delete[]" and remember syntax that way. 😂
too many argument for the operator...error says..it doesn't compile...window 10 visual studio code..why it says so..i typed carefully but cannot overload ostream and istream..just prefixing friend keyword everything becomes good..but why i cant make it without friend function..
@@MikeShah Sir i cannot share screenshot here..can i get mail or someplace where i can send you entire code ..code is not lengthy but i want to ask some technical details in that..like i have question in linked list using class where i have to pass the node as pointer reference although everytime i create new on heap but it gets lost..i really need some way to communicate because i am not satisfied with the others answer..i wont bother you ..i promise..
'operator+' can be thought of as a function, so if you don't specify how to handle that, then the types do not know how to handle add. You can try otherwise and see the compiler errors guide you.
great explanation Mike. I like the way you explain with refering to cppreference page because we get the idea of how we can refer that as we cant remember all the syntax.
Cheers, I appreciate the kind words!
You ended with "thanks for your time folks" to which as a now old looney toons I would answer, no, mister, thank YOU for your amazing effort and superblous education! What you're doing here throughout your charitable human oriented spirit is way more... And gloriously so ... Than state education ever did... Wouldn't it be funny if I was I was in fact a chatgpt bot saying this... Or is it better to say language oriented models or something like that? 😂
Hi! semi-self taught programmer here. I have been expertimenting with C++ for over an year.
Since, I started watching this series... The way I have been thinking about C++ is fundamentally changing(Not because of just this video). I am grateful for these videos Thanks Mike!😄
Cheers!
Thank you very much for this GoG level oldie but Goldie ありがとう ... 谢谢
You are most welcome!
Hi, Good job Mike, I could not understand the error @12:46, postfix operator++ not declared. @12:05 you have declared operator++(). Can you please explain why prefix resolved this. Thanks
I probably should have implemented void operator++() { } for prefix (without any argument). Postfix implementation is supposed to make a copy first, modify the copy, and then return the new copy of the object, whereas prefix (i.e. ++vec3f) should just modify the object -- no need for an argument.
When I had both free and member functions declared, I got a warning saying"... these are ambiguous ...", which makes sense. But unlike your example, running mine resulted in using the free function. That is a bit confusing as I thought member functions trump free functions.
Interesting -- There should be some known order for 'overload resolution' -- see if en.cppreference.com/w/cpp/language/overload_resolution or en.cppreference.com/w/cpp/language/operators has it specified.
@@MikeShah running it on godbolt I still get the free function running. And reading the links and consulting with one of my friends, it does make sense as main is closer to the global scope than the class scope. Anyway, I guess I rather move on and deal with this when/if this comes up in a real world example. Thank you.
@@MindHaunter Interesting, I'll have to look closer at the overload resolution. I might have covered this somewhere as well, but seem to forget the priority off the top of my head. Perhaps it is indeed implementation dependent, though i feel like you'd match the member function first, and then the free function intuitively.
cheers mate
Cheers!
At 16:00 this answers for me 'how to do equivalent Java-like toString() override'. Thank you so much.
Now that I think about it not exactly because in the cpp example it is returning and in the context of an osstream and in Java just returning string but still helpful 🤣
@@damondouglas cheers!
Wonderfully explained as always. Thank You.
Cheers thank you for the kind words!
In one language, APL, there is a user stable "fuzz factor" for comparisons.
Oh neat, that's for numeric types?
@@MikeShah yes.
May I know the reason that @12:11 the return type of prefix increment in cppreference is X& but your implementation is X? Thx
Depends if you want to return by reference, ,or a fresh copy of the object.
Hi Mike
Whenever we do x+rhs.x then how compiler decided that this x we have to consider from myvector object
Compiler knows types of each variable? Could you perhaps restate so I understand better (i.e. Do you mean how does the compiler know "someInt + rhs.x" works)?
@@MikeShah rhs.x is from myvector2 ,but we have not passed any object which refers myvector ,then how that x refers to myvector object
operator+(const Vector& rhs) takes in as a parameter the vector. The 'x' is the current instance (this.x) of myVector1 that is being added.@@kirandhegaskar9298
Ok got it ,thank you Mike
And as always thanks!!
@@guilherme5094 You are most welcome!
Nice video. Somewhere in the middle of this series you might want to do a lesson on testing in C++ and how TDD can help make things easier? I don't know if there is a lightweight testing framework for C++ or something in std that could be purposed for the job? Thanks for the video, really enjoying the series!
Added your request to the wishlist! In this series I will talk about exceptions and assertions at the least. It may belong in a different series on testing, but at some point I would at least talk about Catch2 framework as well.
Hey, I was just working with examples of this code to practice, and I was wondering if I am supposed to declare an operator overload in .h and implement it in .cpp? I'm having some issues with it so I'm not sure.
For example, I used (className operator+(const className& rhs); in .h
But I implement the code in .cpp and I'm getting declaration issues when I use the scope operator like other class functions. So should I implement the code in .h or how does that work? Thanks!
Typically the declaration is in the header file and implementation in the .cpp file. (Note: On occasion 'inline' operations are implemented in the header)
Hey Mike, I would like to know what do you mean by an Helper Function?
What timestamp? By helper function, I usually mean another function to assist in getting work done :)
Hi, I have one question here if we are overloading + operator is it also required to write the assignment operator to collect the result properly.
It's a good question-- the result of operator+ in my demonstration returns a new object. If you assign it, you probably want the '=' operator overloaded. If you overload '=' you probably also want '==' so you can test for equality. I don't know if there's a general name for this, but in general, if you overload +, you probably want to overload '-', '=', and '==' operators at the minimum :)
@@MikeShah Ok, Thanks for the answer
Hi Mike, great video. I have tried to overload the insertion operator and could not get it right cause i have marked my field private. I did some research and found out it is rather done with friend . Any explanation why?
Friend functions allow us to share private data from classes. I will cover this in a video within this classes series in a few lessons :)
@@MikeShah even though the data was public, I had to use friend function to get it working. Hope i'll be able to figure out watching the videos further in the series.
Thanks a lot!
Cheers!
Hello, I am just wondering why operator ++ and operator + do not return a reference ?
You are referring to postfix increment for ++ (keep in mind their is prefix and postfix). postfix increment (e.g. i++) actually returns a new copy that is incremented, so it does not return a reference (See example here: docs.microsoft.com/en-us/cpp/cpp/increment-and-decrement-operator-overloading-cpp?view=msvc-170). Following a similar thought process, operator+ makes a copy. It's a bit strange, but x += y is different than x + y (See bullet point 5 and 6 here: isocpp.org/wiki/faq/operator-overloading#op-ov-rules)
😃 Overloaded 'operator' must be a binary operator. Thank you buddy🙏
Cheers!
that `rhs) const {` syntax feels so random and misplaced. Go guess where it relates to if you see it the first time. Imagining people getting in one place and like "Aha, yeah, let's add const syntax between signature and body to indicate... whatever". And in the one of previous video there was something like `Array(const Array &rhs) = delete;`. It feels in this language you need just ignore what you are reading aloud because it doesn't make sense anyway. On the left hand side it's a function signature, on the right - just a keyword and `=` itself doesn't mean equality or assignment. Or maybe that's the strategy - make things completely weird, so everyone LOL about it and like "yeah, that was good one. Better than `rhs) const {` or delete vs delete[]" and remember syntax that way. 😂
Keep cppreference open at all times 😀
too many argument for the operator...error says..it doesn't compile...window 10 visual studio code..why it says so..i typed carefully but cannot overload ostream and istream..just prefixing friend keyword everything becomes good..but why i cant make it without friend function..
Should be able to make a member, but friend is common. Hmm, maybe post a snippet here and I or someone can help
@@MikeShah Sir i cannot share screenshot here..can i get mail or someplace where i can send you entire code ..code is not lengthy but i want to ask some technical details in that..like i have question in linked list using class where i have to pass the node as pointer reference although everytime i create new on heap but it gets lost..i really need some way to communicate because i am not satisfied with the others answer..i wont bother you ..i promise..
@@shaileshdubey7197 Can post on the courses.mshah.io community snippet of code.
@@MikeShah thank you sir..🙏.
@@MikeShah Sir i have posted my question in c++ section.. please take a look and explain things in simple way like you do
the worst example I've ever seen in my life. you didn't even show why you can't add two objects without operator overloading
'operator+' can be thought of as a function, so if you don't specify how to handle that, then the types do not know how to handle add. You can try otherwise and see the compiler errors guide you.