One possible citation for this page is: https://www.securecoding.cert.org/confluence/display/cplusplus/OOP33-CPP. Do not slice derived objects BudVVeezer (talk)
| This article is rated Start-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | |||||||||||
| |||||||||||
One possible citation for this page is: https://www.securecoding.cert.org/confluence/display/cplusplus/OOP33-CPP.+Do+not+slice+derived+objects BudVVeezer (talk) 18:14, 7 October 2014 (UTC)
I don't have something citable for this or I would add to the article, but slicing is quite danger when there is inheritance from a concrete class. If, for example, you have a concrete class Building and you have a class Skyscraper that inherits from Building, you are likely to have variables of type Building in your program. If you assign a Skyscraper to one of these, you'll have slicing.
On the other hand, if Building were an abstract class, you would never instantiate a Building as such, so this wouldn't be a risk. - Jmabel | Talk 18:18, 8 August 2008 (UTC)
Hi, Jmabel,
I think this article is misleading. It incorrectly implies that "object slicing" is undesirable, when in fact it is the correct and expected behavior.
For example, if a programmer declares a collection by value of Shape class objects, she is explicitly stating that the collection is concerned only with Shapes in general and that it doesn't matter to the program if objects inserted into the collection were originally Circle, Square, or Triangle objects.
If in fact if *does* matter, then it was simply a design error to have used containment 'by value' -- containment 'by reference' (meaning either C++ reference '&' or pointer '*') must be used.
Rather than characterizing this as a C++ language deficiency, I think the article should compare and contrast the C++ language features assignment 'by value' and 'by reference', and provide design guidance regarding when it might be appropriate to use either option.
I'd like to update the article, if you don't object.
Regards, Chris Pearson —Preceding unsigned comment added by ChrisCPearson (talk • contribs) 05:50, 11 September 2009 (UTC)
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.