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
1
vote
2 answers
Virtual functions not executing derived class versions
I have a collection of SceneElements that I want to stream out. This is the aggregate class:
class scene{
public:
vector elements;
void addElement(sceneElement);
void toStream(std::ostream &);
void…

AdamSpurgin
- 951
- 2
- 8
- 28
1
vote
3 answers
C++ slicing causing leak / undefined behavior / crash
Is there any example of the C++ object slicing effect which can cause undefined behavior, memory leak or crash in an otherwise correct set of code? For example when class A and B (inherited from A) are correct and sound, but calling a void f(A a)…

Notinlist
- 16,144
- 10
- 57
- 99
1
vote
2 answers
A potential workaround for C++ slicing?
I have a C++ slicing problem. I'm new to C++, so maybe just too dumb to realize this can't be done.... I have tried a variety of workarounds, and my current best approach looks like below. (I need to do something like this to avoid changing a ton…

John
- 5,735
- 3
- 46
- 62
0
votes
3 answers
Will this result in object slicing?
Say I have an array of pointers to an abstract class:
Piece *pieces[2]; // where piece is an abstract class
And I have two classes that extend Piece called King and Queen. I assign a King to a spot in pieces and Queen to another spot:
pieces[0]…

Austin Moore
- 1,414
- 6
- 20
- 43
0
votes
1 answer
How to assign a subclass to an abstract class pointer in a function?
void Player::move(Board &board, Solver &solver){
Position* best = solver.find_best_move(&board);
cout<<"Score: "<get_score()<get_board()->print_board();
board = *(best->get_board());
Board *…

carboncomputed
- 1,630
- 3
- 20
- 42
0
votes
4 answers
What is hidden slicing copy constructor?
This question is because of this question and the comments.
This example :
#include
struct A {
A(int value) : m_value(value) { }
int m_value;
};
struct B : A {
B(int value) : A (value) { }
};
int main()
{
try {
…

BЈовић
- 62,405
- 41
- 173
- 273
0
votes
1 answer
Preprocessing binary columns in pandas
I am working with data frame in pandas.
import pandas as pd
df = pd.read_csv('Amazon_Historical_StockPrice2.csv',parse_dates=['Date'], index_col='Date')
I need to choose only binary columns for preprocessing. I was trying to make a condition for…

Ольга
- 1
0
votes
2 answers
How to overcome object slicing in repository pattern?
I have the following piece of code:
#include
class AnimalRepository {
public:
virtual ~AnimalRepository() = default;
virtual auto makeSound() -> void {
std::cout << "Null" << std::endl;
}
};
class CatRepository:…

spiklespacklespokle
- 39
- 7
0
votes
1 answer
How can we store object of derived type into pointers of base type?
I am having difficulty in understanding how we can store objects of derived type into pointer of base type.
class Base
{
int a; // An object of A would be of 4 bytes
};
class Derived : public Base
{
int b; //This makes Derived of 8…

Kshitij_9192
- 37
- 1
- 7
0
votes
0 answers
Slicing “From the End” with Negative Indexes
I need to write python code which uses slicing to return "ze" from the string "Malwarez!"
I wrote this:
spam = "Malwarez!"
X = spam[-2:5:-1]
print(X)
However, I just came to the end result by randomly guessing the steps and stops.
Can someone…
user20135069
0
votes
0 answers
How to pull out JSON data using the keyname and indexing across entire array, across multiple unnamed objects
I am tying to pull some data points out of JSON data to create an array or list of data using JSON.parse. Below is the example data. I want to end up with something that returns [5490, 5510, 5520, 5530, 5540] Is it possible to do this using indexing…

Dylan Sinclair
- 3
- 2
0
votes
0 answers
How i can slice Nodeiterator in python ? I
I want to download followers of an instagram account using instaloader ,but the profile i want to scrape contains almost 800 followers and instaloader allows me to scrape 200 followers for single code run and i have to run same code again and…

zohaib hassan
- 11
- 3
0
votes
0 answers
Using object slicing for effective assignment
Let's say I have the definitions
struct A{
int a1 = 0;
int a2 = 1;
};
struct B: public A{
int b1 = 2;
int b2 = 3;
};
And I have a B object that I'm going to call foo, and I want to update its fields inherited from A to the values…

k huang
- 409
- 3
- 10
0
votes
0 answers
Why doesn't Polymorphed function work? c++
I am currently preparing for an exam, by learning Polymorphism.
I wrote this Programm:
#include
#include
using namespace std;
class A {
public:
int x = 3;
virtual void print() { cout << 3 << endl; }
};
class B : public…

NoNToxic
- 1
- 1
0
votes
1 answer
Select by index and by boolean indexing
Input program:
import pandas as pd
df = pd.DataFrame({"A" : ["A0","A1","A2","A3"],
"B" : ["dog","cat","dog","dog"]})
myindexes = pd.Index([1,2])
A B
0 A0 dog
1 A1 cat
2 A2 dog
3 A3 dog
I want to modify df to get…

floupinette
- 150
- 11