a*********e 发帖数: 228 | 1 I have a class A, in the constructor, I say: if somevariable != 2,
delete this. basically, i hope that after calling delete this,
the pointer i make from "new A(int)" becomes NULL, but it turns out
that the pointer is still valid, only the member variable value is
reset to zero. Anybody knows what is going on? Wouldn't delete this
should call the destrcutor of the class? | g*****g 发帖数: 34805 | 2 Very bad design, and it's not gonna work.
try put your condition outside of the class.
You probably want a Factory pattern.
in Factory class, use
if(var==2) {
new Your_class()
}
【在 a*********e 的大作中提到】 : I have a class A, in the constructor, I say: if somevariable != 2, : delete this. basically, i hope that after calling delete this, : the pointer i make from "new A(int)" becomes NULL, but it turns out : that the pointer is still valid, only the member variable value is : reset to zero. Anybody knows what is going on? Wouldn't delete this : should call the destrcutor of the class?
| c********e 发帖数: 383 | 3 your idea itself is wrong.
A * a = new A;
no matter the contructor does delete this or not, the = operator will be
evalutated after.
【在 a*********e 的大作中提到】 : I have a class A, in the constructor, I say: if somevariable != 2, : delete this. basically, i hope that after calling delete this, : the pointer i make from "new A(int)" becomes NULL, but it turns out : that the pointer is still valid, only the member variable value is : reset to zero. Anybody knows what is going on? Wouldn't delete this : should call the destrcutor of the class?
| a*********e 发帖数: 228 | 4 nod, that is the reason why a is not NULL. thanks!
【在 c********e 的大作中提到】 : your idea itself is wrong. : A * a = new A; : no matter the contructor does delete this or not, the = operator will be : evalutated after.
|
|