avatar
delete this problem# Programming - 葵花宝典
a*e
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?
avatar
g*g
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?

avatar
c*e
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?

avatar
a*e
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。