Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 644065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:21:02+00:00 2026-05-13T21:21:02+00:00

Object slicing is some thing that object looses some of its attributes or functions

  • 0

Object slicing is some thing that object looses some of its attributes or functions when a child class is assigned to base class.
Some thing like

Class A{

}
Class B extends A{

}

Class SomeClass{
A a = new A();
B b = new B();

// Some where if might happen like this */
a = b; (Object slicing happens)

}

Do we say Object slicing is any beneficial in any ways?
If yes, can any one please tell me how object slicing be a helpful in development and where it might be helpful?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:21:03+00:00Added an answer on May 13, 2026 at 9:21 pm

    In C++, you should think of an object slice as a conversion from the derived type to the base type[*]. A brand new object is created, which is “inspired by a true story”.

    Sometimes this is something that you would want to do, but the result is not in any sense the same object as the original. When object slicing goes wrong is when people aren’t paying attention, and think it is the same object or a copy of it.

    It’s normally not beneficial. In fact it’s normally done accidentally when someone passes by value when they meant to pass by reference.

    It’s quite hard to come up with an example of when slicing is definitively the right thing to do, because it’s quite hard (especially in C++) to come up with an example where a non-abstract base class is definitively the right thing to do. This is an important design point, and not one to pass over lightly – if you find yourself slicing an object, either deliberately or accidentally, quite likely your object hierarchy is wrong to start with. Either the base class shouldn’t be used as a base class, or else it should have at least one pure virtual function and hence not be sliceable or passable by value.

    So, any example I gave where an object is converted to an object of its base class, would rightly provoke the objection, “hang on a minute, what are you doing inheriting from a concrete class in the first place?”. If slicing is accidental then it’s probably a bug, and if it’s deliberate then it’s probably “code smell”.

    But the answer might be “yes, OK, this shouldn’t really be how things are structured, but given that they are structured that way, I need to convert from the derived class to the base class, and that by definition is a slice”. In that spirit, here’s an example:

    struct Soldier {
        string name;
        string rank;
        string serialNumber;
    };
    
    struct ActiveSoldier : Soldier {
        string currentUnit;
        ActiveSoldier *commandingOfficer; // the design errors multiply!
        int yearsService;
    };
    
    template <typename InputIterator>
    void takePrisoners(InputIterator first, InputIterator last) {
        while (first != last) {
            Soldier s(*first);
            // do some stuff with name, rank and serialNumber
           ++first;
        }
    }
    

    Now, the requirement of the takePrisoners function template is that its parameter be an iterator for a type convertible to Soldier. It doesn’t have to be a derived class, and we don’t directly access the members “name”, etc, so takePrisoners has tried to offer the easiest possible interface to implement given the restrictions (a) should work with Soldier, and (b) should be possible to write other types that it also works with.

    ActiveSoldier is one such other type. For reasons best known only to the author of that class, it has opted to publicly inherit from Soldier rather than providing an overloaded conversion operator. We can argue whether that’s ever a good idea, but let’s suppose we’re stuck with it. Because it’s a derived class, it is convertible to Soldier. That conversion is called a slice. Hence, if we call takePrisoners passing in the begin() and end() iterators for a vector of ActiveSoldiers, then we will slice them.

    You could probably come up with similar examples for an OutputIterator, where the recipient only cares about the base class part of the objects being delivered, and so allows them to be sliced as they’re written to the iterator.

    The reason it’s “code smell” is that we should consider (a) rewriting ActiveSoldier, and (b) changing Soldier so that it can be accessed using functions instead of member access, so that we can abstract that set of functions as an interface that other types can implement independently, so that takePrisoners doesn’t have to convert to Soldier. Either of those would remove the need for a slice, and would have potential benefits for the ease with which our code can be extended in future.

    [*] because it is one. The last two lines below are doing the same thing:

    struct A {
        int value;
        A(int v) : value(v) {}
    };
    
    struct B : A {
        int quantity;
        B(int v, int q) : A(v), quantity(q) {}
    };
    
    int main() {
        int i = 12;  // an integer
        B b(12, 3);  // an instance of B
        A a1 = b;    // (1) convert B to A, also known as "slicing"
        A a2 = i;    // (2) convert int to A, not known as "slicing"
    }
    

    The only difference is that (1) calls A’s copy constructor (that the compiler provides even though the code doesn’t), whereas (2) calls A’s int constructor.

    As someone else said, Java doesn’t do object slicing. If the code you provide were turned into Java, then no kind of object slicing would happen. Java variables are references, not objects, so the postcondition of a = b is just that the variable “a” refers to the same object as the variable “b” – changes via one reference can be seen via the other reference, and so on. They just refer to it by a different type, which is part of polymorphism. A typical analogy for this is that I might think of a person as “my brother”[**], and someone else might think of the same person as “my vicar”. Same object, different interface.

    You can get the Java-like effect in C++ using pointers or references:

    B b(24,7);
    A *a3 = &b; // No slicing - a3 is a pointer to the object b
    A &a4 = b;  // No slicing - a4 is a reference to (pseudonym for) the object b
    

    [**] In point of fact, my brother is not a vicar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.