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
905
votes
18 answers
What is object slicing?
In c++ what is object slicing and when does it occur?

Frankomania
- 9,115
- 3
- 17
- 3
77
votes
6 answers
Store derived class objects in base class variables
I would like to store instances of several classes in a vector. Since all classes inherit from the same base class this should be possible.
Imagine this program:
#include
#include
using namespace std;
class Base
{
public:
…

drakide
- 1,757
- 2
- 15
- 23
72
votes
8 answers
difference between a pointer and reference parameter?
Are these the same:
int foo(bar* p) {
return p->someInt();
}
and
int foo(bar& r) {
return r.someInt();
}
Ignore the null pointer potential. Are these two functions functionally identical no matter if someInt() is virtual or if they are passed…

criddell
- 14,289
- 9
- 41
- 45
44
votes
3 answers
Selecting last n columns and excluding last n columns in dataframe
How do I:
Select last 3 columns in a dataframe and create a new dataframe?
I tried:
y = dataframe.iloc[:,-3:]
Exclude last 3 columns and create a new dataframe?
I tried:
X = dataframe.iloc[:,:-3]
Is this correct?
I am getting array dimensional…

Toly
- 2,981
- 8
- 25
- 35
36
votes
4 answers
Unexpected behavior after assignment of function object to function wrapper
I was searching a bug in an application, which I've finally fixed but didn't understand completely.
The behavior can be reproduced with the following simple program:
#include
#include
#include
struct Foo
{
…

Rabbid76
- 202,892
- 27
- 131
- 174
19
votes
3 answers
Idiomatic way to prevent slicing?
Sometimes it can be an annoyance that c++ defaults to allow slicing. For example
struct foo { int a; };
struct bar : foo { int b; };
int main() {
bar x{1,2};
foo y = x; // <- I dont want this to compile!
}
This compiles and runs as…

463035818_is_not_an_ai
- 109,796
- 11
- 89
- 185
18
votes
4 answers
Is it ever a good idea to put virtual methods on a copyable type?
Have seen some related questions, but not this exact one...
I've treated classes as fitting into a few major categories, let's say these four for simplicity:
Value Classes which have some data and a bunch of operations. They can be copied and…

HostileFork says dont trust SE
- 32,904
- 11
- 98
- 167
16
votes
3 answers
References and Object Slicing
I don't have my Effective C++ with me and this is bugging me so much that I have to ask for my own sanity. Given
class Foo : public Bar{}
void MyFunc(Bar &_input);
If I pass in a Foo, am I tangling with the slicing problem or have I avoided it?

wheaties
- 35,646
- 15
- 94
- 131
15
votes
2 answers
Is object slicing ever useful?
Object slicing happens when we assign or copy an object of derived class to an object of its base class, losing the derived part of it in the process.
It has been explained in more depth here: What is the slicing problem in C++?.
(Myself, I don't…

jrok
- 54,456
- 9
- 109
- 141
12
votes
8 answers
Learning C++: returning references AND getting around slicing
I'm having a devil of a time understanding references. Consider the following code:
class Animal
{
public:
virtual void makeSound() {cout << "rawr" << endl;}
};
class Dog : public Animal
{
public:
virtual void makeSound() {cout << "bark"…

JnBrymn
- 24,245
- 28
- 105
- 147
9
votes
6 answers
overriding virtual function return type differs and is not covariant
Ah, SO came back just in time.
I am getting a strange error:
'B::blah': overriding virtual function return type differs and is not covariant from 'A::blah'
Here is the code causing the problem:
class A {
public:
class Inner { };
virtual…

Seth Carnegie
- 73,875
- 22
- 181
- 249
9
votes
6 answers
Avoiding object slicing
So I am refreshing on C++, and honestly it's been awhile. I made a console pong game as a sort of refresher task and got some input on using polymorphism for my classes to derive from a base "GameObject" (that has some base methods for drawing…

msmith1114
- 2,717
- 3
- 33
- 84
9
votes
3 answers
c++: can vector contain objects of type Derived?
The title pretty much says it all. Basically, is it legal to do this:
class Base {
//stuff
}
class Derived: public Base {
//more stuff
}
vector foo;
Derived bar;
foo.push_back(bar);
Based on other posts I've seen, the following is…

Beezum
- 341
- 1
- 4
- 12
9
votes
4 answers
Exception slicing - is this due to generated copy constructor?
I've just fixed a very subtle bug in our code, caused by slicing of an exception, and I now want to make sure I understand exactly what was happening.
Here's our base exception class, a derived class, and relevant functions:
class…

Michael Kohne
- 11,888
- 3
- 47
- 79
8
votes
8 answers
Why do virtual functions need to be passed with a pointer and not by value(of the object)?
I think I understand the concept of virtual methods and vtables, but I don't understand why there is a difference between passing the object as a pointer(or reference) and passing it by value (which kind of scraps the vtable or something?)
Why would…

Manux
- 3,643
- 4
- 30
- 42