0

I am currently struggling with references and smart pointers with c++, this is my class layout (Everything is child of abstract A)

A
+-B
+-C
+-D

What i want is when I call B().foo(C()) is to add shared pointer of C() to vector> inside of B. and the same for B().foo(D()) however when I call

void foo(const A& item){
    container.push_back(make_shared<A>(item);
}

it seems to strip all non A attributes from item (although function item.bar() still calls overloaded function in item)

it might be that I completely miss the point here, but that is where I am stuck right now

Ladislav Louka
  • 256
  • 4
  • 21
  • I understand object slicing but was more focused on how it works with make_shared / how to avoid it with its use – Ladislav Louka Apr 20 '19 at 16:17
  • 1
    It works the same as everywhere else; if you say "create an object of type A", you get an object of type A. If you want polymorphic copying (a.k.a. "cloning"), you need to add such functions to your types. – molbdnilo Apr 20 '19 at 16:22
  • Thanks that is what I was looking for – Ladislav Louka Apr 20 '19 at 16:26

0 Answers0