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
3
votes
3 answers
C++ polymorphism and slicing
The following code, prints out
Derived
Base
Base
But I need every Derived object put into User::items, call its own print function, but not the base class one. Can I achieve that without using pointers? If it is not possible, how should I write the…

Draco Ater
- 20,820
- 8
- 62
- 86
3
votes
2 answers
Deleting a Base pointer that is pointing to a Derived object
Will this work, or will there be slicing (i.e. only the memory corresponding to that occupied by a Base object will be freed, rather than for the whole Derived object):
Base* ptr = new Derived;
delete ptr;
If not, will this?
delete…

d7samurai
- 3,086
- 2
- 30
- 43
3
votes
2 answers
Object slicing with Queues
Developing on Ubuntu using Eclipse/gcc with -std=c++0x.
I seem to be having an issue with object slicing, that doesn't fall under the other questions I've seen here. I have a very simple base class / child class inheritance model. Base class has…

dcdunne
- 55
- 2
3
votes
3 answers
Creating an array in c++ of type superclass to store subclass objects dynamicaly
Please consider the following code :
#include
using namespace std;
class superclass;
class subclass;
class subclass2;
class superclass
{
public:
unsigned int a;
superclass **superman;
…

Quickpaw
- 77
- 1
- 5
3
votes
2 answers
Slicing in inherited objects
The following cases point out the slicing problem:
During an assignment:
https://stackoverflow.com/a/274634/640639
During a function call:
What is object slicing?
My question is wouldnt both of these cases be solved if the assignment operator and…

Romonov
- 8,145
- 14
- 43
- 55
3
votes
3 answers
Allow operator= being used only between objects of the same class?
I have a class hierarchy and I want to forbid doing this:
Foo *f = new Foo();
Bar *b = new Bar();
f = b;
where Foo is a superclass of Bar. Doing this would slice the Bar part of the object. I know you can solve this by making operator= private,…

Topsic
- 109
- 1
- 9
3
votes
3 answers
How can I make the method of child be called: virtual keyword not working?
The following is my code,
#include
#include
using namespace std;
class TestClass
{
public:
virtual void test(string st1, string st2);
};
class ExtendedTest: public TestClass
{
public:
virtual void test(string st1,…

Janus.Le
- 241
- 4
- 12
3
votes
1 answer
Dynamic casting of stack object fails
I came across an instance the other day where I had a function taking a pointer to a base type which I then needed to up-cast to a derived type to access some additional functionality. However, dynamic_cast failed, which was odd because my type…

user975326
- 657
- 1
- 7
- 22
2
votes
5 answers
C++ inheritance - subclasses constructor call?
I have the following in the main:
Sum *sum = new Sum(Identifier("aNum1"), Identifier("aNum2"));
And my classes are:
class Table {
private:
static map m;
public:
static int lookup(string ident)
{
return…

agouil
- 31
- 4
2
votes
2 answers
C++ copy constructor calling with object slicing
I wrote a C++ code snippet to test which copy constructor will be called during object slicing. The code is as follows:
#include
using namespace std;
class pet
{
public:
pet ()
{
cout << "pet default constructor" << '\n';
}
…

Jacky
- 45
- 6
2
votes
1 answer
To slice or not to slice when passing inheriting reference types
I'm working on some code involving "real-life" as opposed to program-only entities. Let's say that it's a camel-handling library. Now, my library has two camel reference-type classes: Camel and SingingCamel. Camel is a concrete class with no virtual…

einpoklum
- 118,144
- 57
- 340
- 684
2
votes
3 answers
Throw string exception from inherited exception class
I implemented my exception class which inherited from std::exception like the following :
class MyException : public exception
{
public:
MyException(string exc)
{
this->exc = exc;
}
const char * what() const throw ()
{
…

BattleTested_закалённый в бою
- 4,037
- 5
- 26
- 47
2
votes
0 answers
Slicing in Python, HackerRank Common Child
Here is my code to solve a problem of finding the largest sequence of the same letters in two strings in the same order. My approach is basically to see if the letter is in both strings, then on that letter, the comparison string is sliced to avoid…

Melin
- 29
- 2
2
votes
1 answer
Question about constructor matching and implicit conversion
I am doing an old school exam in preparation for my current exam and in one of the questions I am given this code:
#include
using namespace std;
struct A
{
A() : m_a(17) { cout << "A_default" << endl; }
int m_a;
};
struct B1 : A
{
…

Schytheron
- 715
- 8
- 28
2
votes
2 answers
Is object slicing by assigning through a base reference well-defined?
Is the object slicing that occurs when assigning a derived object through a base class reference (without a virtual operator=) a well-defined operation? I.e., is it guaranteed by the standard to leave the derived parts of the object untouched?
In…

drRobertz
- 3,490
- 1
- 12
- 23