C++ Overloading the Assignment Operator  [4]

C++ Overloading the Assignment Operator [4]

Professor Hank Stalica

1 год назад

4,943 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Planet of the Idiots
Planet of the Idiots - 05.11.2023 21:06

best Explanation - THANK u

Ответить
Neil Yoshi
Neil Yoshi - 16.09.2023 20:00

Very useful

Ответить
David Moore
David Moore - 25.05.2023 08:32

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?

Ответить
m.ariuz
m.ariuz - 22.04.2023 20:18

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 << "inside constuctor" << std::endl;' at the assignment 'f=g;' it gets called twice. Why does this happen? It should only return 1 copy.

Ответить
qCNC K
qCNC K - 11.04.2023 15:49

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.

Ответить