Timeline for answer to What is object slicing? by Minok
Current License: CC BY-SA 2.5
Post Revisions
5 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 25, 2014 at 17:24 | comment | added | Minok | I recall from my C++ programming courses in university that there were standing best practices that for every class we created, we were required to write default constructors, copy constructors and assignment operators, as well as a destructor. This way you made sure that copy construction and the like happened the way you needed it to, while writing the class... rather than later on some odd behavior showing up. | |
| Oct 18, 2012 at 3:06 | comment | added | Dude | You're right. Good practice is to use abstract base classes or to restrict the access to copy/assignment. However, it's not so easy to spot once it's there and easy to forget to take care of. Calling virtual methods with sliced *this can make mysterious things happen if you get away without an access violation. | |
| Oct 18, 2012 at 2:51 | comment | added | Dude | protected copy/assignment on the base class and this problem is solved. | |
| Dec 22, 2010 at 11:06 | comment | added | Arafangion | But remember, Minok, that you're NOT passing in a reference of that object. You're passing a NEW copy of that object, but using the base class to copy it in the process. | |
| Jul 24, 2009 at 19:45 | history | answered | Minok | CC BY-SA 2.5 |