0
Default constructor and default assignment operator
I know we have the option to use the default provided class members,delete them or implement them differently, I am interested in understanding when i should reimplement them instead of it being able to , the cases i could think of are when i have a class whose members are dynamically allocated so when copying them i am pointing at the same members location from different objects and as i go out of scope they get double freed ,same for = op other than double free i need to free old fields
3 Antworten
+ 2
There are many other instances when you will need to reimplement the synthesized constructor. The major reason here is it doesn't fit your need.
Says you want a constructor that logs the number of child instances to the stdout, sure you must reimplement. That's just the case.
As for assignment operator, it's more advanced and I hope you understand it.
In c++, there's a different between instantiation and assignment. An object that never existed cannot be assigned a value. There's a long context to this
During assignments, data are either copied or moved. You need to reimplement this operator if you want to be very explicit in the operation done. Again, there are other instances.
+ 1
after wrestling with constructors , you'll probably have to grapple with those other pesky rules(rule of three, rule of five, rule of zero) ...
https://en.cppreference.com/w/cpp/language/rule_of_three.html#:~:text=%5Bedit%5D%20Rule%20of%20three,~rule_of_three()%20//%20I.
https://www.youtube.com/watch?v=una89pkP9ms
it's a deep rabbithole. The youtube video gives a good explanation on why you need to go through this ritual of redefinitions.
0
RuntimeTerror thanks i only was imagining that i need to change them when i am dynamically allocating data basically any other reason i could not think of it causing unexpected errors