0

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 base;
Derived derived;

Base newbaseobj((Base)derived);

this is a classic slicing problem in the first place! I don't see why or how defining a user-defined copy constructor in Base would help

Edit: I'm asking this because these answers: https://stackoverflow.com/a/26040064/1938163 and https://stackoverflow.com/a/26040050/1938163 seemed implying that I need to define/default a copy constructor to deal with object slicing

Community
  • 1
  • 1
Dean
  • 6,610
  • 6
  • 40
  • 90
  • 1
    I'm not sure whether this question is actually a duplicate, but if slicing is the actual problem, then the linked question should help. About the destructor/copy constructor/assign operator: this is called "the rule of three" and is not about slicing but about resource (mostly pointers) handling. – stefaanv Sep 25 '14 at 14:39
  • This is not a duplicate and that definitely doesn't answer my question! – Dean Sep 25 '14 at 14:43
  • Do you mean disable/delete the copy constructor etc? – Neil Kirk Sep 25 '14 at 14:54
  • @user3834459 The rule of 3/5 and object slicing are sort of orthogonal things. Sorry it was unclear for me, what you're actually asking about. – πάντα ῥεῖ Sep 25 '14 at 14:58
  • **No worries, I edited my question entirely to render it (I hope) more clear** – Dean Sep 25 '14 at 15:00
  • It doesn't help. Where did you find any claim that it would? – molbdnilo Sep 25 '14 at 15:14
  • @molbdnilo in this post: http://stackoverflow.com/a/26040064/1938163 – Dean Sep 25 '14 at 15:16
  • 1
    The final link is to an answer I wrote describing the Rule of Three, which has little to do with slicing. As an afterthought, I suggested that "you might consider deleting the copy/move operations to prevent slicing"; but I certainly didn't say (and hopefully didn't imply) anything along the lines of "define/default a copy constructor to deal with object slicing" – Mike Seymour Sep 25 '14 at 15:25
  • Thanks Mike and sorry for getting it wrong then :) – Dean Sep 25 '14 at 15:25

3 Answers3

3

My question is: why is the lack of a copy constructor a problem in a base class regarding object slicing?

It depends what you mean by "the lack of".

If there's genuinely no copy constructor, then there's no problem: the base class can't be copied, and therefore slicing can't occur.

There could be a problem if it simply doesn't declare one; in that case, one is implicitly generated, and can be used to slice a derived-class object.

I don't see why or how defining a user-defined copy constructor in Base would help

Deleting it would help by preventing copying, and therefore slicing, altogether. Declaring it protected would help by only allowing the base class to be copied as part of a derived object (except within members of derived classes), preventing most cases of slicing.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

re

why is the lack of a copy constructor a problem in a base class regarding object slicing

it isn’t, in general.

a user defined copy constructor can help to prevent slicing by being inaccessible, thus preventing copy initialization (full armour requires then also taking charge of copy assignment), or (don't do this) it can detect the slicing dynamically, for a polymorphic type, and in that case allowing general copying.

but an accessible ordinary copy constructor doesn’t prevent slicing. it can at most help with detecting copy operations in debugging.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • so what this guy wrote is wrong? http://stackoverflow.com/a/26040064/1938163 by reading it, it seemed to me that copy constructor should be defaulted/defined just to deal with object slicing – Dean Sep 25 '14 at 15:17
  • @user3834459: That's not what it says at all. It says "the default-generated one is highly likely not suitable", "you should probably just forbid it" and "I guess you could maybe make it protected". – Mike Seymour Sep 25 '14 at 15:20
  • @user3834459: no, as far as I can see what the Good Robot wrote is correct. he did not write that the copy constructor should be defaulted/defined to deal with object slicing. – Cheers and hth. - Alf Sep 25 '14 at 15:21
  • thanks, now I get it. Makes sense to avoid copying the base object, not "defining a copy constructor in the base to deal with object slicing" – Dean Sep 25 '14 at 15:24
1

Object slicing comes into picture when inheritance(more specifically Polymophism) is involved in your design. When you are passing derived class object by value to a function which is expecting Base class object then slicing takes place. Whether you define copy constructor on your own OR use compiler generated slicing will take place any way.

Pointers are one way to solve slicing problem and in this case passing derived to base won't create slicing problem ( assuming polymorphism is involved) and there would be no call to copy constructor of any class as pointers are involved.

Hope I answered your queries...

ravi
  • 10,994
  • 1
  • 18
  • 36
  • Thanks for your help! Indeed object slicing will happen anyway unless I take some precautions (e.g. protect/private/delete the copy constr.) – Dean Sep 25 '14 at 15:35