Classes part 6 - Operator overloading | Modern Cpp Series Ep. 42

preview_player
Показать описание

►Lesson Description: In this lesson I show how to perform operator overloading in C++ so that you can use your objects with operators like +, -, *, / , etc. In fact, you can even use other common operators like the stream operators, brackets, new, and delete. Whether or not you should use operator overloading is a style question, and general guidelines I have heard recommend against using operator overloading for anything other than mathematical types where it makes the code more intuitive. That said, occassionally it is useful to overload somethings like the less than comparison operator for use in std::sort, or the == operator for comparisons.

►Please like and subscribe to help the channel!
Рекомендации по теме
Комментарии
Автор

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.

RahulBochare-zm
Автор

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? 😂

VoidloniXaarii
Автор

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!😄

jrohit
Автор

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.

MindHaunter
Автор

Thank you very much for this GoG level oldie but Goldie ありがとう ... 谢谢

VoidloniXaarii
Автор

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

syedimadhaqqi
Автор

At 16:00 this answers for me 'how to do equivalent Java-like toString() override'. Thank you so much.

damondouglas
Автор

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!

AlexanderWhillas
Автор

Wonderfully explained as always. Thank You.

dhanushs
Автор

In one language, APL, there is a user stable "fuzz factor" for comparisons.

johnmckown
Автор

May I know the reason that @12:11 the return type of prefix increment in cppreference is X& but your implementation is X? Thx

sugoilang
Автор

Hey Mike, I would like to know what do you mean by an Helper Function?

bxbx
Автор

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!

thicc_tiffanyplayz
Автор

Hi Mike
Whenever we do x+rhs.x then how compiler decided that this x we have to consider from myvector object

kirandhegaskar
Автор

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.

TechTricks
Автор

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?

blaisofotso
Автор

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. 😂

Kirfx
Автор

Hello, I am just wondering why operator ++ and operator + do not return a reference ?

nemolovie
Автор

😃 Overloaded 'operator' must be a binary operator. Thank you buddy🙏

joshbrice
Автор

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..

shaileshdubey