由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - delete this problem
相关主题
一个简单的小问题copy constructor 问题
问个C++中重复删除指针的问题Why this is a dangling pointer
请问delete的问题c++ 里面 this pointer 是完全 un necessary 的吗?
请问以下代码有什么错误?大家觉得C++复杂在哪里?
请教一个基本的constructor和destrcutor问题这段code有啥问题?
菜鸟请教smart pointer定义linked list最后一行什么意思?
C++ Q05: pointer to constant variablepthread_create inside a constructor
pointer to base class = new derived, what will happend??关于数组动态分配的疑问???
相关话题的讨论汇总
话题: delete话题: class话题: null话题: pointer话题: problem
进入Programming版参与讨论
1 (共1页)
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.

1 (共1页)
进入Programming版参与讨论
相关主题
关于数组动态分配的疑问???请教一个基本的constructor和destrcutor问题
C 多线程的一个问题菜鸟请教smart pointer
一个小问题C++ Q05: pointer to constant variable
作为返回值得实参是用指针还是引用比较好?pointer to base class = new derived, what will happend??
一个简单的小问题copy constructor 问题
问个C++中重复删除指针的问题Why this is a dangling pointer
请问delete的问题c++ 里面 this pointer 是完全 un necessary 的吗?
请问以下代码有什么错误?大家觉得C++复杂在哪里?
相关话题的讨论汇总
话题: delete话题: class话题: null话题: pointer话题: problem