Classes part 3 - Copy Constructor, Copy Assignment(Deep and Shallow Copy) | Modern Cpp Series Ep. 39

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

►Lesson Description: In this lesson I cover the copy constructor, and copy assignment operator. I start by showing how the compiler gives you these two special functions for classes for free, using the compiler generated ones. Unfortunately, this is not sufficient, as when we have to dynamically allocate memory, the trivial copy can result in problems like double frees or unwanted sharing of data. Next I show you how to avoid making these 'shallow' copies, and write your own copy constructor and copy-assignment operators to perform a deep copy.

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

Does "Array myArray2 = new Array(myArray); " triggers copy-constructor as well?? If yes, I assume the difference is allocating myArray2's memory on heap?

kenk
Автор

Wow, I dont understand how this is not 100k+ views, this is by far the best explanation I"ve seen of this concept !

noelgallagher
Автор

This is the best c++ playlist on youtube by far!!!!

astbast
Автор

Thanks for publishing these videos to public. Your teaching style is fantastic.

atlasflare
Автор

Omgg 😮 this is c++ goldmine so excited to complete this playlist.

Request: please make videos on building tools using c++, I always want to develop something using c++ but dunno how to start, thanks man.

crazymemes
Автор

Fantastic tutorial, as I'm learning UE5 C++, this tutorial is helping me a lot to understand what is happening there

punchline
Автор

You are a great teacher thank you so much for this valuable content
please continue

sallaklamhayyen
Автор

So thankful for all your amazing lessons

VoidloniXaarii
Автор

Damn ! Such a clear and wonderful explanation.! You deserve much more views and subs sir...!
It would be very useful if you could do a video on namespace, scope of variablesand functions in namespace, inline keyword and how scopes and lifetimes of identifiers defined in different circumstances and the best practices to be used in header files!
And im a bit confused between difference between file scope vs global scope and gobal namespace parent namespaces. It'd be of great help if you could enlighten me ! Thanks a lot!!

vimalathithand
Автор

Interesting channel. C++ topics are very well explained here.

embeddedMikeTV
Автор

Mike, a quick question. In the copy assignment function why do we delete[] before assigning new heap area for the new data object? delete[] operation marks the area as free right, thereby making the data pointed by the original object a dangling pointer? Am I missing something here? Please help! Thanks

Quancept
Автор

Thanks for making this video. It's an excellent explanation.

reptilicusrex
Автор

Just wanted to stop by and see you're awesome! I just started learning C++ coming from python and Go and your videos have really helped me. They are currently my main source of learning! Thanks for the great content.

mfc
Автор

Very neatly explained as always. Thank You

dhanushs
Автор

Hi Mike, Thank you for the video. This is the best video that i watched which explains both copy constructor and copy assignment operator clearly.

sowmya
Автор

My go-to resource for C++ tutorials, you're the best! 📚🙌

deepblackoutlaw
Автор

Thanks, Prof
Can’t get enough
MasterClass
Kept revisiting
Quite clear in my head

joebosah
Автор

Thanks for the clear explanation
But do we need to delete the allocated memory and reallocate it again in the copy assignment operator?

hossamalzomor
Автор

Thinking back to previous video, what if the pointer were a unique pointer? Before you made the copy constructor?

johnmckown
Автор

Hey Mike! Thanks for the videos can you explain why we need to return as Array&, we can simply use Array right?

sauravshaw