C++ Overloading the Assignment Operator [4]
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
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.
You're very welcome!
This was extremely helpful thank you very much!
best Explanation - THANK u
Glad it was helpful!
Thanks professor
Any time
Thank you so much for this
No problem 😊
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?
Very useful
Glad you think so!
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
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?
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;
@@ProfessorHankStalica Thanks
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
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.
@@ProfessorHankStalica Thank you very much.😊