Copy object using pointer (templates)
Posted
by
Azodious
on Stack Overflow
See other posts from Stack Overflow
or by Azodious
Published on 2011-06-24T05:13:08Z
Indexed on
2011/06/24
8:22 UTC
Read the original article
Hit count: 172
How the push_back
of stl::vector
is implemented so it can make copy of any datatype .. may be pointer, double pointer and so on ...
I'm implementing a template class having a function push_back
almost similar to vector. Within this method a copy of argument should be inserted in internal memory allocated memory. but the argument is a pointer. (an object pointer).
Can you pls tell how to create copy from pointer. so that if i delete the pointer in caller still the copy exists in my template class?
Code base is as follows:
template<typename T>
class Vector
{
public:
void push_back(const T& val_in)
{
T* a = *(new T(val_in));
m_pData[SIZE++] = a;
}
}
Caller:
Vector<MyClass*> v(3);
MyClass* a = new MyClass();
a->a = 0;
a->b = .5;
v.push_back(a);
delete a;
Thanks.
© Stack Overflow or respective owner