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
4
votes
2 answers

What are the undesirable results the author is talking about?

This example was taken from Bruce Eckel's "Thinking in C++" Chapter 14, Section "Upcasting and the Copy Constructor". #include using namespace std; class Parent { int i; public: Parent(int ii) : i(ii) { cout << "Parent(int…
Belloc
  • 6,318
  • 3
  • 22
  • 52
4
votes
3 answers

C++: Is there a more elegant solution to this (multiple dispatch) runtime polymorphism?

The main problem is simple, really. Given a base (more abstract) class and multiple derived ones that need to interact with each other, how do you go about doing it? To give a more concrete example, here is an implementation with hitboxes for a 2d…
4
votes
1 answer

C++ final class and slicing idiom

I happened to be browsing the source for mongoDB, and found this interesting construct: class NonspecificAssertionException final : public AssertionException { public: using AssertionException::AssertionException; private: void…
user9987379
4
votes
2 answers

Can std::move cause slicing when moving into l-value reference?

See the following code: #include #include class Parent { public: Parent() = default; virtual ~Parent() = default; Parent(const Parent& pr) : i{pr.i} {std::cout << "Parent copy constructor\n";} Parent&…
ToddR
  • 300
  • 1
  • 11
4
votes
1 answer

How to prevent move slicing?

When a derived class instance is passed as a r-value parent reference to an unsuspecting method, the latter can legally change the parent's contents, causing incoherence with any extra data stored in the actual object. Therefore a class designed for…
The Vee
  • 11,420
  • 5
  • 27
  • 60
4
votes
1 answer

Polymorphism: member acces and getter give different results

Here is the code: #include #include #include class Parent { public: virtual void whatAmI(){std::cout << "A Parent" << std::endl;} virtual long getValue(){std::cout << "value from Parent " << std::endl; return…
Rrr
  • 53
  • 4
4
votes
3 answers

Does std::move result in slicing?

For example, in unique_ptr = new deriv; std::vector>.push_back(std::move(deriv)); will deriv be sliced to type unique_ptr?
user383352
  • 5,033
  • 4
  • 25
  • 20
4
votes
2 answers

Slicing in C++ where I am wrong?

I read about the slicing problem in C++ and I tried some examples (I am from Java background). Unfortunatelly, I do not understand some of the behaviour. Currently, I am stucked in this example (alternated example from Efficent C++ Third Edition).…
napets
  • 133
  • 1
  • 7
4
votes
2 answers

Preventing slicing in copy constructor

I want to copy a vector of type Foo objects but the objects can be several different derived types of Foo. I can't figure out how to copy without slicing. Here's my toy code #include "stdafx.h" #include #include #include…
Phlox Midas
  • 4,093
  • 4
  • 35
  • 56
3
votes
3 answers

Are subclasses that only change the members valid practice?

Lets say I have a class A that has a member of type int. I have a class B which is a subclass of A. B is meant to initialize the members to some state and has no other purpose. #include #include struct A { int someInt; …
Raildex
  • 3,406
  • 1
  • 18
  • 42
3
votes
5 answers

Smart Pointers In C++

Say we have a base class and a derived. So: class base { protected: ~base(){ //... } // ... }; class derived : public base { // ... }; And now say that we have this code using the above classes…
K-RAN
  • 896
  • 1
  • 13
  • 26
3
votes
2 answers

C++: object slicing and exceptions

In an interview I was asked why catching exceptions by value can be a problem and I answered that this can cause object slicing. And this is what I find in the Internet, for example here: https://www.viva64.com/en/w/v746/ But now I am trying to…
Andrey Rubliov
  • 1,359
  • 2
  • 17
  • 24
3
votes
2 answers

c++ prevent object slicing in std::vector

I would like to store multiple classes with the same base class in a std::vector. After some research, it became apparent to me that I have to use pointers to prevent object slicing. However, when I create the vector, add elements to it and return…
Rheel
  • 410
  • 4
  • 19
3
votes
8 answers

How do I store different classes in a single vector?

So I have a vector full of all the objects for my game; things like the player object, enemy object, walls, etc... All things in the vector are children of Framework, so I made the vector type Framework because that was the closest thing to a…
null
  • 548
  • 2
  • 6
  • 17
3
votes
2 answers

c++ polymorphism, name resolution for derived class

class Base { public: virtual void f(float) { cout << "Base::f(float)\n"; } }; class Derived : public Base { public: virtual void f(int) { cout << "Derived::f(int)\n"; } }; int main() { Derived *d = new Derived(); Base *b =…
legokangpalla
  • 495
  • 5
  • 20
1 2
3
14 15