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

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ม.ค. 2025

ความคิดเห็น • 61

  • @RahulBochare-z5m
    @RahulBochare-z5m หลายเดือนก่อน +2

    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.

    • @MikeShah
      @MikeShah  หลายเดือนก่อน +1

      Cheers, I appreciate the kind words!

  • @VoidloniXaarii
    @VoidloniXaarii 10 หลายเดือนก่อน +4

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

  • @jrohit1110
    @jrohit1110 ปีที่แล้ว +4

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

  • @VoidloniXaarii
    @VoidloniXaarii 10 หลายเดือนก่อน +1

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

    • @MikeShah
      @MikeShah  10 หลายเดือนก่อน +1

      You are most welcome!

  • @syedimadhaqqi4340
    @syedimadhaqqi4340 ปีที่แล้ว +1

    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

    • @MikeShah
      @MikeShah  ปีที่แล้ว +1

      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.

  • @MindHaunter
    @MindHaunter 3 หลายเดือนก่อน +1

    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.

    • @MikeShah
      @MikeShah  3 หลายเดือนก่อน

      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.

    • @MindHaunter
      @MindHaunter 3 หลายเดือนก่อน +1

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

    • @MikeShah
      @MikeShah  3 หลายเดือนก่อน +1

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

  • @-vz-
    @-vz- 21 วันที่ผ่านมา +1

    cheers mate

    • @MikeShah
      @MikeShah  21 วันที่ผ่านมา

      Cheers!

  • @damondouglas
    @damondouglas 2 ปีที่แล้ว

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

    • @damondouglas
      @damondouglas 2 ปีที่แล้ว

      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 🤣

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +1

      @@damondouglas cheers!

  • @dhanushs1802
    @dhanushs1802 2 ปีที่แล้ว +2

    Wonderfully explained as always. Thank You.

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว

      Cheers thank you for the kind words!

  • @johnmckown1267
    @johnmckown1267 6 หลายเดือนก่อน +1

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

    • @MikeShah
      @MikeShah  6 หลายเดือนก่อน

      Oh neat, that's for numeric types?

    • @johnmckown1267
      @johnmckown1267 6 หลายเดือนก่อน +1

      @@MikeShah yes.

  • @sugoilang
    @sugoilang ปีที่แล้ว

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

    • @MikeShah
      @MikeShah  ปีที่แล้ว +1

      Depends if you want to return by reference, ,or a fresh copy of the object.

  • @kirandhegaskar9298
    @kirandhegaskar9298 11 หลายเดือนก่อน

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

    • @MikeShah
      @MikeShah  11 หลายเดือนก่อน

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

    • @kirandhegaskar9298
      @kirandhegaskar9298 11 หลายเดือนก่อน

      @@MikeShah rhs.x is from myvector2 ,but we have not passed any object which refers myvector ,then how that x refers to myvector object

    • @MikeShah
      @MikeShah  11 หลายเดือนก่อน

      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

    • @kirandhegaskar9298
      @kirandhegaskar9298 11 หลายเดือนก่อน

      Ok got it ,thank you Mike

  • @guilherme5094
    @guilherme5094 15 วันที่ผ่านมา +1

    And as always thanks!!

    • @MikeShah
      @MikeShah  15 วันที่ผ่านมา

      @@guilherme5094 You are most welcome!

  • @AlexanderWhillas
    @AlexanderWhillas 2 ปีที่แล้ว +4

    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!

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +3

      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.

  • @thicc_tiffanyplayz
    @thicc_tiffanyplayz 10 หลายเดือนก่อน +1

    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!

    • @MikeShah
      @MikeShah  10 หลายเดือนก่อน

      Typically the declaration is in the header file and implementation in the .cpp file. (Note: On occasion 'inline' operations are implemented in the header)

  • @b0xb0x87
    @b0xb0x87 ปีที่แล้ว +1

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

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      What timestamp? By helper function, I usually mean another function to assist in getting work done :)

  • @TechTricks1992
    @TechTricks1992 ปีที่แล้ว +1

    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.

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      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 :)

    • @TechTricks1992
      @TechTricks1992 ปีที่แล้ว +1

      @@MikeShah Ok, Thanks for the answer

  • @blaisofotso3439
    @blaisofotso3439 2 ปีที่แล้ว +2

    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
      @MikeShah  2 ปีที่แล้ว +1

      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 :)

    • @dhanushs1802
      @dhanushs1802 2 ปีที่แล้ว +1

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

  • @Andrei-cv1kn
    @Andrei-cv1kn 2 ปีที่แล้ว +2

    Thanks a lot!

  • @nemolovie1715
    @nemolovie1715 2 ปีที่แล้ว

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

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +1

      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)

  • @joshbrice5678
    @joshbrice5678 2 ปีที่แล้ว +2

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

  • @Kirfx
    @Kirfx ปีที่แล้ว +1

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

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      Keep cppreference open at all times 😀

  • @shaileshdubey7197
    @shaileshdubey7197 ปีที่แล้ว

    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
      @MikeShah  ปีที่แล้ว +1

      Should be able to make a member, but friend is common. Hmm, maybe post a snippet here and I or someone can help

    • @shaileshdubey7197
      @shaileshdubey7197 ปีที่แล้ว +1

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

    • @MikeShah
      @MikeShah  ปีที่แล้ว +1

      @@shaileshdubey7197 Can post on the courses.mshah.io community snippet of code.

    • @shaileshdubey7197
      @shaileshdubey7197 ปีที่แล้ว +1

      @@MikeShah thank you sir..🙏.

    • @shaileshdubey7197
      @shaileshdubey7197 ปีที่แล้ว

      @@MikeShah Sir i have posted my question in c++ section.. please take a look and explain things in simple way like you do

  • @Принуждениекмиру-ь2ц
    @Принуждениекмиру-ь2ц 8 หลายเดือนก่อน

    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

    • @MikeShah
      @MikeShah  8 หลายเดือนก่อน

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