C++ Overloading the Assignment Operator [4]

Sdílet
Vložit
  • čas přidán 29. 08. 2024
  • Learn how to overload the copy assignment operator ( c++ operator= ) for your classes. Find out why you need an overloaded assignment and how to implement one safely. This C++ tutorial for beginners will demonstrate an example class in a program written in Visual Studio 2022 C++ .
    // Join the new Programming for Everyone Community Forum:
    professorhank....
    // Learn More //
    More About Classes playlist: • CH14: More About Classes
    // Consider supporting this channel in multiple ways
    ko-fi.com/prof...
    paypal.me/hank...
    Bitcoin: 177wfkQwzXiC8o2whQMVpSyuWUs95krKYB

Komentáře • 19

  • @qcnck2776
    @qcnck2776 Před rokem +7

    Thanks so much for explaining the logic behind how the assignment overload is constructed: thinking of it as a function really helps in understanding the process.
    A great video, as always.

  • @skymaster7295
    @skymaster7295 Před 4 měsíci

    This was extremely helpful thank you very much!

  • @mba2ceo
    @mba2ceo Před 9 měsíci +1

    best Explanation - THANK u

  • @da_gamer2345
    @da_gamer2345 Před 5 měsíci +1

    Thanks professor

  • @AlexTrouman-oi1yp
    @AlexTrouman-oi1yp Před 8 měsíci +1

    Thank you so much for this

  • @faizaniftikhar
    @faizaniftikhar Před 5 měsíci

    what i dont understand is when I overload = without const param, a=b works, but to make a=b=c to work, I need to send param as const. why so?

  • @kidyoshi007
    @kidyoshi007 Před 11 měsíci

    Very useful

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před 11 měsíci

      Glad you think so!

    • @kidyoshi007
      @kidyoshi007 Před 11 měsíci

      Yeah i had an assignment which was to create a array based stack using dynamic memory and when you were describing what "this" does, it really helped, i finished the lab in C++ and got a 35/35@@ProfessorHankStalica

  • @davidmoore2699
    @davidmoore2699 Před rokem

    Thanks for these videos. I have a question about when to use a copy constructor and when to overload the assignment operator. Is the difference that you use a copy constructor when you are going to initialize a new instance of the class with the values of an existing instance of that class and you override the assignment operator when you are making one existing instance of a class equal to another existing instance of that class?

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před rokem +1

      Copy constructor is executed by statements such as:
      Foo f(g);
      or
      Foo f = g;
      Assignment operator is executed by a statement such as:
      x = y;

    • @davidmoore2699
      @davidmoore2699 Před rokem

      @@ProfessorHankStalica Thanks

  • @mariuz-xn9pi
    @mariuz-xn9pi Před rokem

    Thank you for the nice video.
    I have 2 questions:
    1: If i leave out the copy constructor the code works without error, but the values seem to be garbage. My guess would be that the constructor just gets skipped. But creating an empty class c: Foo f(5), g(6), h(8), c(); seems like weird behaviour. (Warning that empty paranteses where disambiguated as a function declaration). Which leads me to think that there is no way to skip the constructor. Would be nice if you could provide a short explanation.
    2: If i add the following line in the copy constructor: 'std::cout

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před rokem +3

      If you got garbage values then your variables weren't assigned a value. If you don't have a constructor, that would explain that. There is always a constructor whether or not you explicitly write one. If you don't write it, the compiler will add a do-nothing constructor automatically. Said constructor won't contain any assignment statements so your variables won't be assigned anything.
      Well, it's not f=g that's invoking the copy constructor as no new objects are created with that statement. Copy constructors are only invoked in one of two ways:
      Foo f = g;
      or
      Foo f(g);
      f = g; invokes the assignment operator, operator=.
      So, you'd have to look else where as to the construction of the object. If you have f and g, then it's likely you are seeing it twice, once for each time f and g were instantiated.

    • @mariuz-xn9pi
      @mariuz-xn9pi Před rokem +1

      @@ProfessorHankStalica Thank you very much.😊