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
2
votes
2 answers
How to implement ICloneable without inviting future object-slicing
My question is about how to implement the classic ICloneable interface in such a way that it won't lead to inadvertent object-slicing when a future programmer isn't paying close attention. Here's an example of the kind of programming error I'd like…

Jeremy Friesner
- 70,199
- 15
- 131
- 234
2
votes
3 answers
How to slice a dictionary in python and Robot-framework?
Slicing is available for lists in python
list1 =[1,2,3,4,5,6]
list1[:3]
[1, 2, 3]
Similarly, slicing or anything similar to that available for dictionary ?
dict1 = {1":a",2:"b",3:"c",4:"d",5:"e"}
I would like to get any 3 (can be random) elements…

StackGuru
- 471
- 1
- 9
- 25
2
votes
1 answer
How to elegantly slice the object
So basically I have a class SomethingSwimming and a derived class Ship. I want to implement that upon some event a Ship may lose its special qualities (such as holding lobsters hostage) and become just another SomethingSwimming object.
To…

Trotom
- 61
- 6
2
votes
1 answer
Xarray indexing / slicing to account for seasons
I am using xarray to proccess a pretty long data set. It has been very much fun and I was able to find everything I needed in the online documentation. However now I try to plot data for summer and winter, and it won't work. While I found a lot of…

Gabriel
- 67
- 5
2
votes
2 answers
Runtime introspection in C++
I have a class that looks like this:
class MyClass {
public:
void doSomething() { // nothing here };
}
and it also has a subclass that looks like this
class MyChildClass : MyClass {
public:
void doSomething() { // actual code here…

Christian Daley
- 779
- 2
- 9
- 13
2
votes
1 answer
When I get a case of `slicing`?
Let's look the next code (for example):
class A {
int n;
public:
int f() const { return n; }
void set(int i) { n = i; }
};
class B : public A {
public:
int g() const { return f()+1; }
};
void h(const A& a) {
a.f();
}
int main()…

StackUser
- 309
- 2
- 10
2
votes
2 answers
C++ - Object slicing even after using pointers
I have a base class Shape, and a derived class Circle which inherits Shape publically:
class Circle : public Shape
I made a C++ vector of Shape pointers, and I assigned Circle pointers to them. I'd read up a lot on object slicing so expected the…

Lazar Lazarov
- 33
- 2
2
votes
2 answers
How to return the leaves of a struct as vector in Matlab?
Often I need to access the leaves of data in a structured array for calculations.
How is this best done in Matlab 2017b?
% Minimal working example:
egg(1).weight = 30;
egg(2).weight = 33;
egg(3).weight = 34;
someeggs = mean([egg.weight]) % works…

Jonas Stein
- 6,826
- 7
- 40
- 72
2
votes
1 answer
Is object slicing dependent on the constructor implementation?
While experimenting with the concepts of object slicing and polymorphism, I came up with this code example, which works as I was expecting: the function call operator of the derived FunctorTrue class is called -instead of that of the parent Functor…

levelont
- 584
- 4
- 12
2
votes
4 answers
Calling a derived method on an element of a base vector (example given)
Suppose that I have the following structure of classes. I want to be able to determine of what class type the element in my Animal vector is, so that I may perform subclass-specific methods on it. The example below should demonstrate:
#include…

Schreib
- 21
- 1
2
votes
2 answers
Object slicing : pass Derived as Base by value - safe or dangerous?
I am studying about when/why object slicing is dangerous.
I read a great link about what is safe slicing VS dangerous slicing.
Here is what I can summarize (roughly speaking):-
Safe when the base type is value (e.g. A).
Dangerous when the…

javaLover
- 6,347
- 2
- 22
- 67
2
votes
1 answer
Is it possible to implement a copyable_unique_ptr that is not affected by slicing?
Regardless of the fact that copying a unique_ptr makes sense or not*, I tried to implement this kind of class, simply wrapping a std::unique_ptr, and got into difficulty exactly where the copy is taken, in the case of a smart pointer to base and the…

rubenvb
- 74,642
- 33
- 187
- 332
2
votes
3 answers
Curious case of C++ Object slicing behaviour on Base class static reference data member
I am experiencing a curious case of Object slicing. I am working on project where I need singleton classes so my Base and derived class both are singleton. Following sample case describes my situation.
This is my Base class
// Base.h
class Base
{
…

Dr. Xperience
- 475
- 1
- 5
- 18
2
votes
3 answers
Why isn't this method call virtual like I was expecting?
I want to ask what happen, when I use virtual functions without pointers ? for example:
#include
using namespace std;
class Parent
{
public:
Parent(int i) { }
virtual void f() { cout<<"Parent"<

yProgrammer
- 173
- 1
- 4
2
votes
1 answer
STL Container with iterator and inheritance
Here is a sample C++ question to find out the outcome.
#include
#include
class A
{
public:
A(int n = 0) : m_n(n) { }
public:
virtual int f() const { return m_n; }
virtual ~A() { }
protected:
int m_n;
};
class…

Muralitharan Perumal
- 163
- 1
- 8