Object slicing refers to assignment by value of a sub-class instance to a super-class instance,thereby losing part of the information i.e. the sub-class specific data members are ignored.
Questions tagged [object-slicing]
220 questions
7
votes
1 answer
Why unique_ptr doesn't prevent slicing of custom deleter?
The behavior of std::unique_ptr with custom deleter is based on the static type of the deleter. No polymorphism, no runtime behavior based on actual deleter passed in runtime, as derived deleter provided is being sliced to the static type of the…

Amir Kirsh
- 12,564
- 41
- 74
7
votes
1 answer
C++ slicing in Java / C#
Can C++ slicing apply to other languages too, like Java/C#?

yesraaj
- 46,370
- 69
- 194
- 251
7
votes
1 answer
Prevent derived class from casting to base
I have
class Rect{
// stuff
};
and
class SpecialRect:public Rect{
private:
operator const Rect(){return *this;} // No implicits casts to Rect
public:
// stuff
};
SpecialRect inherits all the properties and methods from Rect…

tru7
- 6,348
- 5
- 35
- 59
7
votes
2 answers
Object slicing when using std::enable_if
I'm attempting to use std::enable_if to specialise a class if one of it's subclasses has a specific member function defined. Otherwise it should use a default implementation that is defined in the base class.
#include
#include…

Simon Gibbons
- 6,969
- 1
- 21
- 34
7
votes
5 answers
Does polymorphism work in C++ without pointers / references?
Possible Duplicate:
Virtual Functions Object Slicing
let's consider:
#include
#include
using namespace std;
struct A {
virtual void do_it() { cout << "A" << endl; }
};
struct B : public A {
virtual void do_it() {…

lezebulon
- 7,607
- 11
- 42
- 73
6
votes
2 answers
Move semantics in derived-to-base class conversions
Consider the following class Buffer, which contains an std::vector object:
#include
#include
class Buffer {
std::vector buf_;
protected:
Buffer(std::byte val): buf_(1024, val) {}
};
Now, consider the function…

JFMR
- 23,265
- 4
- 52
- 76
6
votes
3 answers
Slicing and operator overloading in C++
Background Info
I've been programming in Java for a while, and I've only switched over to C++ just a few months ago, so I apologize if the answer is just something silly that I missed! Now that that's all been said, it is time for the issue at hand!…

gen220
- 63
- 1
- 4
5
votes
1 answer
How come `e.what()` prints "bad allocation"?
The new expression in the try block throws a bad_allocexception in my computer.
Note that the catch clause receives an exception object by value, not by reference. How come e.what() prints "bad allocation" ? I thought it would be sliced.
#include…

Belloc
- 6,318
- 3
- 22
- 52
5
votes
3 answers
How object slicing can result in memory corruption?
The C++ expert & D language creator Walter Bright says that:
The slicing problem is serious because it can result in memory
corruption, and it is very difficult to guarantee a program does not
suffer from it. To design it out of the language,…

Destructor
- 14,123
- 11
- 61
- 126
5
votes
2 answers
Virtual functions using base and derived objects
I have read about vtable and have understood the concept for base class pointers pointing to base and derived class objects.
Can someone explain the case how vtable is created when both base class and derived class are objects and derived class…

Sumanth V
- 71
- 1
- 6
5
votes
1 answer
Avoiding Object Slicing C++
I have an issue where I'd like to copy an object, but want to avoid slicing it.
DerivedObj derivedObj;
myFunc(derivedObj);
void myFunc(MyObj &obj)
{
MyObj *saveForLater = new MyObj(obj); // slices my object
// ... //
}
Is there a way to get…
user245019
5
votes
5 answers
Avoid slicing of exception types (C++)
I am designing an exception hierarchy in C++ for my library. The "hierarchy" is 4 classes derived from std::runtime_error. I would like to avoid the slicing problem for the exception classes so made the copy constructors protected. But apparently…

shojtsy
- 1,710
- 3
- 17
- 24
5
votes
1 answer
Why does static_cast still work with null pointers in spite of slicing?
If we use multiple inheritance, slicing will make the addresses to parent objects differ from the address to leaf objects:
struct X {int x};
struct Y {int y};
struct Z : X, Y {int z};
So if we have a Z object z, its address &z will not coincide…

bitmask
- 32,434
- 14
- 99
- 159
4
votes
3 answers
C# vs C++ - Types, inheritance and vtable
I'm having trouble understanding what causes this difference between C++ and C#.
First we have an example in which the base class contains a virtual function.
class Base
{
protected:
int super;
public:
virtual int f() = 0;
};
class Derived…

user1202032
- 1,430
- 3
- 15
- 36
4
votes
2 answers
Is it safe to downcast objects (not pointers) to their known derived type?
Would there be a danger of slicing
result Compare(const Osp::Base::Object &obj1, const Osp::Base::Object &obj2, int &cmp) const {
cmp = ((const Block)obj1).NumSuperBlocks() - ((const Block)obj2).NumSuperBlocks();
}
Where
class Block : Object…

John
- 6,433
- 7
- 47
- 82