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
0
votes
3 answers
How does the lack of a copy constructor relate to object slicing?
Warning: This is NOT a duplicate of What is object slicing? , I already read it and it doesn't clarify my issue
My question is: why is the lack of a copy constructor a problem in a base class regarding object slicing? If I do something like
Base…

Dean
- 6,610
- 6
- 40
- 90
0
votes
1 answer
Viewing local as derived type in Locals window when type is shared_ptr
When I have a shared_ptr to a derived type, but the type of the shared_ptr is to the base type, I cannot see anything but the base type's members in the Locals window of Visual Studio (as if the object was sliced).
Below is a very small program that…

Steve
- 6,334
- 4
- 39
- 67
0
votes
1 answer
How to make a vector of *objects without being demoted to base class
In my class implementation, I have something like this:
base class
class swcWidget :
public swcRectangle
{
public:
swcWidget();
virtual ~swcWidget();
void update(float dt);
protected:
inline virtual void oPaintOnTop() {…

mr5
- 3,438
- 3
- 40
- 57
0
votes
3 answers
constructors calling subclass
I have a (partally implemented) class hierarchy where
template {
class data {
data ( string s ) {}; // loads from file
...
}
class image: public data {
image ( string s ) {}; // loads from file
…

alle_meije
- 2,424
- 1
- 19
- 40
0
votes
1 answer
Is `auto x = f()` better than `T x = f()`?
In another topic someone suggested using
auto x = f();
instead of
T x = f();
(if the signature of f is T f()). They pointed out this prevents silent object slicing if someone happens to change f to U f(), where U descends from T.
It makes sense…

ricab
- 2,697
- 4
- 23
- 28
0
votes
1 answer
Why does slicing a non-root base class produce the correct answer?
I have a case in my code where I'd like to use object slicing, but I'm trying to determine if it's safe or smart to do it. In trying to determine this, I ran the following example:
#include
using namespace std;
class Dog{
public:
…

Homer6
- 15,034
- 11
- 61
- 81
0
votes
3 answers
Proper way to use operator = in derived class and slicing
So I have a base class that has a private member and a derived class that also has some member, the base class defines an operator =, and this is my questions:
Is this the proper way to do this or is there a better way?
Do I commit any slicing along…

Ravid Goldenberg
- 2,119
- 4
- 39
- 59
0
votes
2 answers
c++ object factory function
Lets say I have the following data structures:
struct Base
{
Base(const int id, const std::string &name, const std::string &category):
id(id), name(name), category(category) {}
int id;
std::string name;
std::string…

rem45acp
- 469
- 5
- 16
0
votes
1 answer
Explicit Slicing a Derived Object
I have some class structure for the project I build for my company. At some point, I've seen a "usually unwanted" slicing of derived object could actually make my code efficient. Please observe:
class Base {
private:
int myType; //some…

Ahmet Ipkin
- 882
- 1
- 10
- 20
0
votes
2 answers
Debug assertion failed BLOCK_TYPE_IS_VALID(pHead->nblockuse) from Deconstructor
I am quite lost right now. I made a vector class. Everything works how I would like it to work, until the end. When the destructor is called I get an error message: Debug assertion failed BLOCK_TYPE_IS_VALID(pHead->nblockuse). I've seen quite a…

SirRupertIII
- 12,324
- 20
- 72
- 121
-1
votes
1 answer
How to print a particular numeric value from a variable content in python
When I run this code:
from luno_python.client import Client
import json
c = Client(api_key_id='', api_key_secret='')
try:
bal = c.get_balances(assets='NGN')
print(bal)
except Exception as e:
print(e)
I get this…

Jideofor5050
- 13
- 3
-1
votes
1 answer
Object slicing in Inventory system
I'm working on an inventory system for a game and I'm hitting a brick wall with object slicing; I'm losing variables on a reference to a derived class.
Below is an excerpt in which a T-shirt is created in the main game file, and then passed to a…

Natharantos
- 1
- 3
-1
votes
1 answer
How to init a struct that includes a slice for Google auto generated lib
I'm trying to make a Google Analytics Go library based off the auto generated package generated here
I have authenticated, got account summaries etc. so all is well until I try to construct a reporting request.
I am trying to init a struct…

MarkeD
- 2,500
- 2
- 21
- 35
-1
votes
4 answers
Python returns first and last item of a sequence exchanged
I need to create a function to slice a sequence in order that first and last item is exchanged and the middle stays in the middle. It needs to be able to handle string/list/tuples. I am having trouble with typeerrors - cant add list + int.…

Iansberg
- 89
- 1
- 1
- 6
-1
votes
2 answers
Avoiding object-slicing in vector>
I'm storing my game's states (collections of entities, essentially) in a vector of shared pointers. When adding states to the vector, the derived part of the states is lost and they revert to the base state class. It all compiles fine, but when I…

Inigo Selwood
- 822
- 9
- 20