Jump to content

Talk:Object slicing

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Possible Citation

[edit]

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)[reply]

Inheritance from a concrete class

[edit]

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)[reply]

Misleading article

[edit]

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 (talkcontribs) 05:50, 11 September 2009 (UTC)[reply]

References that supports this point of view : http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=0358DCEE3E77E550E2DD12C4562A2D85?doi=10.1.1.55.2458&rep=rep1&type=pdf (The Object-Slicing Technique: A Flexible Object Representation and Its Evaluation (1995) by Harumi Kuno , Young-gook Ra , Elke A. ) http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.55.2458 Rundensteineras in which slicing is used as a technique in object database modelling, and https://books.google.fr/books?id=e2JtvBp7pm8C&printsec=frontcover&dq=Object-oriented+analysis+and+design+odell+martin+object+slicing&hl=fr&sa=X&ved=0CCAQ6AEwAGoVChMI_sqX26KLyAIVRr0aCh1o4wMQ#v=onepage&q=slicing&f=falseAdvanced Object-Oriented Analysis and Design Using UML , James J. Odell w where it is noted as a technique to implement some models. (there is an earlier version of the book cited in the first article) @ChrisCPearson: You're welcome to extend and correct the article, I just discover the notion :) TomT0m (talk) 18:40, 22 September 2015 (UTC)[reply]