10:20 No! The standard mentions that if you do Base *p = new Derived. : delete p; then it is undefined behaviour, if Base does not have a virtual destructor. That is it. But if you never do this with your classes => you need not have a virtual Base destructor. An interesting example is std::unary_function and std::binary_function. They can be inherited from. And what do they do? They contain no data. All they do is typedef some lines based on arguments. That is it, nothing else. This is so certain algorithms work smoothly with them.
3:26: No! The rule of 3 is : destructor, copy constructor & copy assignment operator. It is not the default constructor. The rule of 5 is : same as rule of 3 + move constructor & move assignment operator If you need to "undo" something in the destructor, free a resource, you probably need to write the other special functions if the compiler-generated code does not do the right thing
True, I didn't claim that though, but maybe it was misleading. "The compiler is doing the right thing" meant that the copy and move constructors/assignment operators will deal with the builtin types correctly.
Oh! primitive data member like int will not be default constructed with the default constructor with MyClass(), but with MyClass{}. So, we shall add a constructor if there is primitive data members.
I think it's a really handy rule. If I need to write one constructor, I also write the other ones. Most of the time you can get away with writing no constructor at all.
If you have a custom destructor, you probably require a custom copy/move constructor and assignment operators. Custom constructors that doesn't require a custom destructor (Lets say, a class of a few ints) doesn't usually need to have the other functions cause the compiler will do the best thing.
That's very good advice, I wished someone would have told me that when I started out with C++ .
10:20 No!
The standard mentions that if you do
Base *p = new Derived.
:
delete p;
then it is undefined behaviour, if Base does not have a virtual destructor. That is it.
But if you never do this with your classes => you need not have a virtual Base destructor.
An interesting example is std::unary_function and std::binary_function. They can be inherited from.
And what do they do? They contain no data. All they do is typedef some lines based on arguments. That is it, nothing else.
This is so certain algorithms work smoothly with them.
You are right about the standard.
I still think it is best practice to declare a virtual destructor for
3:26: No!
The rule of 3 is : destructor, copy constructor & copy assignment operator. It is not the default constructor.
The rule of 5 is : same as rule of 3 + move constructor & move assignment operator
If you need to "undo" something in the destructor, free a resource, you probably need to write the other special functions if the compiler-generated code does not do the right thing
True, thanks for taking the time to correct the mistake!
awesome!! lernt Something new in this video!!! thanks Zen
Hallo, could you make a video on the topic of multithreading? I think a lot of people would be interested.
I'll think of something this weekend :-)
Hey! Cool and useful video, it reminds me about "Effective C++" book
That’s one of the best books about C++!
Learned a few things :) thnx!
1:08: The default constructor will not initialise builtin types on the stack, they will have any old value.
True, I didn't claim that though, but maybe it was misleading.
"The compiler is doing the right thing" meant that the copy and move constructors/assignment operators will deal with the builtin types correctly.
Cool stuffs 🤗
Thanks 😁
Oh! primitive data member like int will not be default constructed with the default constructor with MyClass(), but with MyClass{}. So, we shall add a constructor if there is primitive data members.
is it really neccesary to use the rule of 5/3/0, instead i can use the basic constructor and destructor
I think it's a really handy rule. If I need to write one constructor, I also write the other ones. Most of the time you can get away with writing no constructor at all.
@@ZenSepiol thanks
If you have a custom destructor, you probably require a custom copy/move constructor and assignment operators. Custom constructors that doesn't require a custom destructor (Lets say, a class of a few ints) doesn't usually need to have the other functions cause the compiler will do the best thing.
thanks for the video
You're welcome