I have basically this setup:
class B { /* ... */};
class C1 : public B { /* ... */};
class C2 : public B { /* ... */};
class X
{
std::vector<shared_ptr<B>> m_vec;
void addToVector(B* b);
}
addToVector
cant know how many classes derive from B and should not care. It will be called like this:
someFunction() {
C1 tmp;
/* do something with tmp */
m_myX.addToVector(&tmp);
}
so at the end of someFunction
, tmp is getting out of scope and will be deleted. addToVector
has to push_back a shared_ptr to a copy of tmp into the vector, but how can it do that?
void X::addToVector(B* b)
{
int i = sizeof(b); // allways sizeof(B) :(
shared_ptr<B> np(b);
m_vec.push_back(np); // garbage collected after calling fn returns :(
}
What it should do is:
- make a copy of the object b is pointing to by calling the copy constructor/operator of the correct class
- push_back a shared_ptr to that copy into the vector.
How can i do this?