Am I deleting this properly?

Posted by atch on Stack Overflow See other posts from Stack Overflow or by atch
Published on 2010-04-06T19:35:39Z Indexed on 2010/04/06 19:43 UTC
Read the original article Hit count: 488

Filed under:
|

I have some struct:

struct A
{
 const char* name_;
 A* left_;
 A* right_;
 A(const char* name):name_(name),
      left_(nullptr),
      right_(nullptr){}
 A(const A&);
 //A(const A*);//ToDo
 A& operator=(const A&);
 ~A()
 {
  /*ToDo*/
 };
};
/*Just to compile*/
A& A::operator=(const A& pattern)
{

 //check for self-assignment
 if (this != &pattern) 
 {
  void* p = new char[sizeof(A)];
 }
 return *this;
}

A::A(const A& pat)
{
 void* p = new char[sizeof(A)];
 A* tmp = new (p) A("tmp");
 tmp->~A();
 delete tmp;//I WONDER IF HERE I SHOULD USE DIFFERENT delete[]?
}

int _tmain(int argc, _TCHAR* argv[])
{
 A a("a");
 A b = a;
 cin.get();
 return 0;
}

Guys I know this is far from ideal and far from finished. But please don't tell me the answer how to do it properly. I'm trying to figure it out myself. The only think I would like to know if I'm deleting my memory in proper way.
Thanks.

© Stack Overflow or respective owner

Related posts about c++

Related posts about delete