防止删除 [英] Preventing delete
问题描述
我一直认为无法删除一个const指针但是下面的
代码在g ++和visual net下编译:
班级假人
{
公开:
假人(){}
~Dummy(){}
};
int main(int,char **)
{
const Dummy * const p = new Dummy();
删除p;
返回0;
}
问题是:我怎样才能禁止用户删除一些数据?
处理指针?下面是编译的代码示例,但是
将导致段错误。我该如何预防呢?
#include< iostream>
#include< string.h>
using namespace std;
class Invoice
{
public:
Invoice(char *标签)
{
_label = strdup(label);
}
~发票()
{
delete [] _label;
}
const char * const label()const
{
返回_label;
}
私人:
char * _label;
};
int main(int,char **)
{
发票d(火车) ;);
delete [] d.label();
cout<< d.label()<< endl;
返回0;
}
Hi,
I long thought one could not delete a const pointer but the following
code compiles under g++ and visual net:
class Dummy
{
public:
Dummy() {}
~Dummy() {}
};
int main(int,char**)
{
const Dummy* const p = new Dummy();
delete p;
return 0;
}
The question is: how can I forbid the user to delete some data I an
handling a pointer to? Here is an example of code that does compile but
will lead to segfault. How can I prevent it?
#include <iostream>
#include <string.h>
using namespace std;
class Invoice
{
public:
Invoice(char* label)
{
_label = strdup(label);
}
~Invoice()
{
delete [] _label;
}
const char* const label() const
{
return _label;
}
private:
char* _label;
};
int main(int,char**)
{
Invoice d("train");
delete [] d.label();
cout<<d.label()<<endl;
return 0;
}
推荐答案
XavierDécoret写道:
Xavier Décoret wrote:
问题是:我怎样才能禁止用户删除一些处理指针的数据?以下是编译的代码示例,但
将导致段错误。如何防止它?
The question is: how can I forbid the user to delete some data I an
handling a pointer to? Here is an example of code that does compile but
will lead to segfault. How can I prevent it?
使析构函数变为私有或受保护,并让用户调用你提供的
自定义函数删除。
Make the destructor private or protected, and make the user call a
custom function that you supply to do the deleting.
XavierDécoret写道:
Xavier Décoret wrote:
我一直认为无法删除一个const指针,但下面的代码在g ++和visual net下编译:
class Dummy
{
public:
Dummy(){}
~Dummy(){}
};
int main(int,char **)
const Dummy * const p = new Dummy();
删除p;
返回0;
}
问题是:我怎么能禁止用户删除一些数据我处理指针?以下是编译的代码示例,但
将导致段错误。如何防止它?
#include< iostream>
#include< string.h>
使用命名空间std;
班级发票
{
公开:
发票(char *标签)
{
_label = strdup(label);
}
〜发票()
{
删除[] _label;
}
const char * const label()const
{
返回_label;
}
私人:
char * _label;
};
int main(int,char **)
{
发票d (train);
删除[] d.label();
cout<< d.label()<< endl;
>返回0;
}
Hi,
I long thought one could not delete a const pointer but the following
code compiles under g++ and visual net:
class Dummy
{
public:
Dummy() {}
~Dummy() {}
};
int main(int,char**)
{
const Dummy* const p = new Dummy();
delete p;
return 0;
}
The question is: how can I forbid the user to delete some data I an
handling a pointer to? Here is an example of code that does compile but
will lead to segfault. How can I prevent it?
#include <iostream>
#include <string.h>
using namespace std;
class Invoice
{
public:
Invoice(char* label)
{
_label = strdup(label);
}
~Invoice()
{
delete [] _label;
}
const char* const label() const
{
return _label;
}
private:
char* _label;
};
int main(int,char**)
{
Invoice d("train");
delete [] d.label();
cout<<d.label()<<endl;
return 0;
}
您是纯粹出于学术兴趣吗?因为现实世界对于特定代码的答案是,对于班级设计师来说:不要
暴露你班级的内部并且对于客户代码:
不要删除你不拥有的记忆。
换句话说,你能提供一个吗?问题的更现实的例子
你试图解决。
Gavin Deane
Are you asking purely out of academic interest? Because the real world
answer to that particular code is, for the class designer: "don''t
expose the internals of your class like that" and for the client code:
"don''t delete memory you don''t own".
In other words, can you provide a more realistic example of the problem
you are trying to solve.
Gavin Deane
XavierDécoret写道:
Xavier Décoret wrote:
我一直以为无法删除一个const指针但是下面的代码在g ++和visual net下编译:
班级假人
{
公开:
假人(){}
〜假人(){}
};
int main(int,char **)
{const Dummy * const p = new Dummy();
删除p;
返回0 ;
}
问题是:我怎样才能禁止用户删除一些处理指针的数据?以下是编译的代码示例,但
将导致段错误。如何防止它?
Hi,
I long thought one could not delete a const pointer but the following
code compiles under g++ and visual net:
class Dummy
{
public:
Dummy() {}
~Dummy() {}
};
int main(int,char**)
{
const Dummy* const p = new Dummy();
delete p;
return 0;
}
The question is: how can I forbid the user to delete some data I an
handling a pointer to? Here is an example of code that does compile but
will lead to segfault. How can I prevent it?
创建自己的类型并覆盖操作员删除?
Ben Pope
-
我不仅仅是一个数字。对很多人来说,我被称为字符串...
Create your own type and override operator delete?
Ben Pope
--
I''m not just a number. To many, I''m known as a string...
这篇关于防止删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!