Questions tagged [object-slicing]

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.

220 questions
1
vote
2 answers

python slicing does not give key error even when the column is missing

I have a pandas dataframe with 10 keys. If I try to access a column that is not present, even then it returns a NaN for this. I was expecting a KeyError. How is pandas not able to identify the missing column ? In the example below, vendor_id is a…
ForeverLearner
  • 1,901
  • 2
  • 28
  • 51
1
vote
1 answer

C++ CRTP derived class object slicing

I have base class Account and derived class Student and Teacher. I implement read/write in the CRTP so that I dont need to write the function in each class. Strangely there is object slicing in the read function and *this used in the function, but…
wthe22
  • 77
  • 1
  • 3
  • 8
1
vote
1 answer

Using object slicing to reliably copy from one of multiple base classes

I have been reading this answer which explained how slicing will modify only one part of an object. I'm wondering if the slicing behaviour explained in the treacherous case is guaranteed or undefined. Given the following class structures (which I…
aerobot
  • 195
  • 1
  • 8
1
vote
3 answers

A way how to overcome object slicing

Is there a way how to step away , overcome object slicing without using new keyword as paramater to function? I have base object class Person{ public: Person(string name , int age ){ this -> name = name; this ->…
Darlyn
  • 4,715
  • 12
  • 40
  • 90
1
vote
2 answers

Dynamic array of objects of parent class to hold child objects

I have a Mammal parent class. Dog, Cat, Lion are the sub-classes. I am using vectors to hold all the sub-classes as Mammal object as such vector v; And using this line to append new objects to the vector. v.push_back(new Dog(Name, Blue,…
Gavin
  • 2,784
  • 6
  • 41
  • 78
1
vote
4 answers

Why can "template void f(T& t)" override functions in T but "template void f(T t)" cannot?

I have a class A with child class B, and want to make a template function to call virtual functions of A and B: #include class A{ public: virtual void test(){ printf("A\n"); } }; class B:public A{ public: virtual void…
ggrr
  • 7,737
  • 5
  • 31
  • 53
1
vote
3 answers

confusion in constructor calling in virtual function

Confusion in constructor calling via temporary object as argument in a function #include using namespace std; class Base { protected: int i; public: Base() { cout<<"\n Default constructor of Base \n"; …
Nadeem Bhati
  • 739
  • 1
  • 8
  • 13
1
vote
2 answers

Object slicing without additional instance methods and variables

I was wondering if I can't just ignore object slicing in a situation similar to the following: class Base { private: int8_t data[128]; // other variables protected: Base(args) : args(args) { } void setData(uint8_t i, int8_t d) {…
Jack
  • 131,802
  • 30
  • 241
  • 343
1
vote
1 answer

Why does slicing occur exactly?

I understand slicing chops off the additional sub class-specific parts of an object, when we assign a super class to a sub class, like so: Sub mySub; Super mySuper = &mySub; // mySuper DOESN'T contain any sub class behaviour and if we did: Sub…
user997112
  • 29,025
  • 43
  • 182
  • 361
1
vote
2 answers

Factory pattern with abstract base classes - return by ref or by value? Problems with scoping and slicing

I have a type hierarchy similar to the code sample below, and I'm trying to instantiate them via the factory pattern (or, to be pedantic, rather the builder pattern, as my factory takes input from an XML document... but I digress). However I try to…
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
1
vote
1 answer

Is legal use initializer_list to initialize an object with derived types?

Well, maybe from the title is not clear what I'm actually asking. I have a class with an initializer-list constructor std::initializer_list. Is legal initialize it with an initializer list of objects of class D, where D is derived from…
Gonmator
  • 760
  • 6
  • 15
1
vote
3 answers

Why does adding a : to any out of range index on a list slice not return out of range?

Why does any slice higher than the length of a list minus 1, not return an error if there is a colon after it? Example: IN: a= [1,2,3] IN: a[999] OUT: IndexError: list index out of range IN:a[999:] OUT: [] Thanks!
KGS
  • 277
  • 2
  • 4
  • 11
1
vote
1 answer

Object slicing in private inheritance

Why object slicing does not takes place in private inheritance? Static_cast gives error in such cases? I understand that the private inheritance does not hold “is - a” relationship between its inheritance classes. Does this has something to do with…
Bhupesh Pant
  • 4,053
  • 5
  • 45
  • 70
1
vote
2 answers

Copy a vector of pointers without slicing

I need to copy a vector of polymorphic objects, in the new vector there should be pointers to the same polymorphic types just not pointing to the same data, but instead they should point to new data on the heap. That data needs to be set to the same…
1
vote
1 answer

C++ Is derived object data guaranteed valid after a base object std::swap?

is it safe to do something like this: class Base { public: int x; }; class Derived : public Base { public: int y; }; Base b; Derived d; b.x = 1; d.x = 2; d.y = 3; std::swap(b,d); At this point is it guaranteed that the derived info d.y is still…
user2672807
  • 1
  • 2
  • 7
  • 20