Is this dangerous by any means? I have no knowledge of other way of doing it but it seems very suspicious.
class cA
{
public:
cA(){}
~cA(){}
int a;
//say class have tons of members..
};
int _tmain( int argc, _TCHAR* argv[] )
{
cA obj;
cA *ptr = new cA;
*ptr = obj;
//ofc there is a 'delete ptr;' after
}
If I remember right on C++ this means that an object of cA
will be created and ptr
will point to it, I have to do this to insert on long life containers ( vector<cA*>
).
Is copying the contents of obj from the stack to the heap that way valid?
edit possible solution?
class cA
{
public:
cA(){}
~cA(){}
int a;
void Copy( cA & ref )
{
a = ref.a;
}
};
int _tmain( int argc, _TCHAR* argv[] )
{
cA obj;
cA *ptr = new cA;
ptr->Copy( obj );