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
3 answers
convert Reference to shared pointer
I have a function like
void Element::setNodes(const BaseClass& input0, const BaseClass& input1)
This is called by passing a derived class.
setInputNodes(DerivedClass1, DerivedClass2)
The trouble I have is that I want to store the nodes in a…

user1876942
- 1,411
- 2
- 20
- 32
2
votes
1 answer
multiple conditioned slicing (pandas dataframe)
I have a dataframe that has various columns and rows of data. I want to select all rows where the Year column = 2015 and Month column = 7.
The following works:
new_result.loc[new_result['Year'] == 2015,:].loc[new_result['Month'] == 7,:]
However, is…

codingknob
- 11,108
- 25
- 89
- 126
2
votes
2 answers
How do i correctly add shared_pointers to probably derived classes into a std::vector?
I have basically this setup:
class B { /* ... */};
class C1 : public B { /* ... */};
class C2 : public B { /* ... */};
class X
{
std::vector> m_vec;
void addToVector(B* b);
}
addToVector cant know how many classes derive from B…

rhavin
- 1,512
- 1
- 12
- 33
2
votes
1 answer
Why does this work? (multiple inheritance, slicing)
Consider this example:
#include
using namespace std;
class A
{
public:
int x;
};
class B
{
public:
int y;
B() { y = 0; }
B(int var): y(var) {}
};
class C : public A, public B
{
public:
void assignB(B x)
{
…

velis
- 8,747
- 4
- 44
- 64
2
votes
1 answer
c++ call to base class method slices object
I have something like this:
#include
class X;
class A {
public:
virtual void bar(X &x);
};
class B : public A {
public:
};
class X {
public:
void foo(A &a) { std::cout << "foo A" << std::endl; }
void foo(B &b) { std::cout <<…

Luke Skywalker
- 1,464
- 3
- 17
- 35
2
votes
2 answers
Object slicing within a two-dimensional array in c++
Hello I am having a small issue with storing derived type objects within a 2-dimensional array of the base type without loosing the derived type stored in the array.
For example there is the following Base and Derived class:
class Base{
}
class…

Baraa
- 687
- 1
- 9
- 26
2
votes
2 answers
Pointers and Object slicing
I'm learning the hard way about object slicing, and I'm wondering if it is possible for a pointer to ever be object sliced. In other words:
Can pointers ever be victims of object slicing or are you always safe from object slicing as long as you're…

Jason Maldonado
- 802
- 1
- 8
- 9
2
votes
1 answer
Conversion operator is slicing my object
I'm getting unexpected behavior from the following code:
struct Base
{
Base() {}
virtual ~Base() {}
virtual void foo() const = 0;
protected:
Base(const Base &) {}
};
struct Derived : public Base
{
Derived() {}
…

kede
- 244
- 1
- 8
2
votes
2 answers
Is it possible for slicing to occur with Smart Pointers?
If I understand slicing correctly I don't think this could happen with pointers or smart pointers. For example, if you had:
class A
{
int something;
};
class B : public A
{
int stuff;
int morestuff;
};
int main()
{
std::shared_ptr b(new…

Seth
- 8,213
- 14
- 71
- 103
2
votes
2 answers
Does the header used to create a library have to be the same as the header using the library
After compiling the source to .o files and using "ar rcs libMyLibrarylib.a *.o" to make the library I am getting segfaults because I am using a header file with the member variables and private functions striped out. When I use the exact same header…

eliteslayer
- 190
- 6
2
votes
2 answers
Collection of objects of more than one type
Is there any non-awful way to have a collection of objects of more than one type? I'm perfectly happy to derive each type from a common base. I need sensible semantics so the collection can be copied, assigned, and so on.
Obviously, I can't just use…

David Schwartz
- 179,497
- 17
- 214
- 278
1
vote
1 answer
AttributeError: 'str' object has no attribute 'str' when used in a user defined function
def region_df(df):
if (df["Region New"] == "OTHER" and df[['COUNTRY NAME']].notnull().all()):
return df["REGION NEWER"]
elif (df["Region New"] == "OTHER" and (df["national Code"].str[:2] == "4A" or df["national Code"][:1]== "3") ):
…

aryastark
- 181
- 1
- 4
1
vote
1 answer
c++ std::exception "what()" message will always only print "std::exception" in a custom exception - why can't I get it to print the contents?
if I do this:
//... in .h header:
class MyCustomException : public std::exception{
private:
string message;
public:
MyCustomException (string msg); //in .cpp implements "message = msg"
~MyCustomException() throw(){ }
string…

Andy
- 33
- 3
1
vote
0 answers
How to fix C++ object slicing in a mixed array?
In my C++ I would like to print out cars and lorrys (derived from the vehicle class). Here are the definitions of the classes:
class Vehicle
{
int speed;
int entryTime;
int currentPositionInLane;
int…

jdsflk
- 417
- 9
- 23
1
vote
0 answers
Creating queue of objects derived from base class in c++
So, in short, I am writing a rather simple calculator (for reverse Polish notation). My base class is Symbol, it is a pure virtual class, and I have bunch of other classes:
Operand: inherits from Symbol
Number: inherits from Operand
Constant:…

Kombajn
- 121
- 2