What a mess!
The whole program is very hard to read because of the choice of identifier names to start with:
#ifndef DELETE
#define DELETE(var) delete var, var = NULL
#endif
I find that very ugly.
When using classes it seems very un-nessacery. You could use it where a variables is going out of scope but it is a waster of time in the destructor. I think it would be asier to wrap the code in some smart pointer:
class Teste
{
private:
Teste *_Z;
public:
Teste()
~Teste() // Delete the _Z pointer.
Teste *Z();
void Z(Teste *value);
};
Ok. You have a pointer member that you delete in the destructor.
This means you are taking ownership of the pointer. This means that the ule of four applies (similar to the rule of three but applicable to ownership rules). This means you basically need to write 4 methods or the compiler generated versions will mess up your code. The methods you should write are:
A Normal (or default constructor)
A Copy constructor
An Assignment operator
A destructor.
Your code only has two of these. You need to write the other two.
Or your object should not take ownership of the RAW pointer. ie. use a Smart Pointer.
Teste *_Z;
This is not allowed.
Identifiers beginning with an underscore and a capitol letter are reserved.
You run the risk of an OS macro messing up your code. Stop using an underscore as the first character of identifiers.
~Teste(){
if (_Z != NULL)
DELETE(_Z);
}
This is not needed. Asimple delete _Z would have been fine.
_Z is going out of scope because it is in the destructor so no need to set it to NULL.
The delete operator handles NULL pointers just fine.
~Test()
{ delete _Z;
}
Teste *Z(){
_Z = new Teste;
return _Z;
}
What happens if you call Z() multiple times (PS putting the * next to the Z rather than next to the Teste make it hard to read).
Each time you call Z() the member variable _Z is given a new value. But what happens to the old value? Basically you are leaking it. Also by returning a pointer to an object owned
inside Teste you are giving somebody else the opportunity to abuse the object (delete it etc). This is not good. There is no clear ownership indicated by this method.
Teste& Z()
{
delete _Z; // Destroy the old value
_Z = new Teste; // Allocate a new value.
return *_Z; // Return a reference. This indicates you are retaining ownership.
// Thus any user is not allowed to delete it.
// Also you should note in the docs that it is only valid
// until the next not const call on the object
}
void Z(Teste *value){
value->AnyNum = 100;
*_Z = *value;
}
You are copying the content of a newly constructed object (that contains a pointer) into another dynamically created object!
What happens if _Z had not been allocated first. The constructor sets it to NULL so there is no guarantee that it has a valid value.
Any object you allocate you should also delete. But here value is dynamically allocated passed into Z but never freed. The reason you get away with this is because the pointer is c
opied into _Z and _Z is deleted when its destructor is destroyed.
Teste *b = new Teste, *a;
That's really heard to read. Don;t be lazy write it out properly.
This is considered bad style and you would never get past any code review with that.
Teste* b = new Teste;
Teste* a; // Why not set it to NULL
a = b->Z();
Getting ab object for a. But who was destroying the object a or b?
b->Z(new Teste);
It just gets too convoluted after that.