防止在SmartPointer上删除 [英] Preventing delete on a SmartPointer

查看:55
本文介绍了防止在SmartPointer上删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在Bill Hubauer(*)提出的实施后实施了一个SmartPointer课程。但我也覆盖了运算符*()


模板< class ObjectType>

类SmartPointer

{

public:

operator ObjectType *()const

{return Pointer; }

....

私人:

ObjectType *指针;

};


现在我的编译器可以编译:


SmartPointer< Object> s =新对象;

删除s;


有没有办法防止这种情况?


谢谢,

Mathieu


(*)
http://groups.google.com/group/comp....3ddc38a827a930


完整源代码位于:
http://svn.sourceforge.net/viewcvs.c...ter.h?view=log

Hi,

I have implemented a SmartPointer class following the implementation
proposed by Bill Hubauer(*). But I also override the operator * ()

template<class ObjectType>
class SmartPointer
{
public:
operator ObjectType * () const
{ return Pointer; }
....
private:
ObjectType* Pointer;
};

Now my compiler can compile:

SmartPointer<Object> s = new Object;
delete s;

Is there a way to prevent that ?

Thanks,
Mathieu

(*)
http://groups.google.com/group/comp....3ddc38a827a930

Full source code is at:
http://svn.sourceforge.net/viewcvs.c...ter.h?view=log

推荐答案

在文章< 11 ********************** @ h76g2000cwa.googlegroups .com> ;,

" ;马修" <毫安*************** @ gmail.com>写道:
In article <11**********************@h76g2000cwa.googlegroups .com>,
"mathieu" <ma***************@gmail.com> wrote:


我在Bill Hubauer(*)提出的实施后实施了一个SmartPointer课程。但我也覆盖了运算符*()

模板<类ObjectType>
类SmartPointer
公共:
运算符ObjectType *()const
{返回指针; }
...
私有:
ObjectType *指针;
};
现在我的编译器可以编译:

智能指针<对象> s = new Object;
删除s;

有没有办法防止这种情况?
Hi,

I have implemented a SmartPointer class following the implementation
proposed by Bill Hubauer(*). But I also override the operator * ()

template<class ObjectType>
class SmartPointer
{
public:
operator ObjectType * () const
{ return Pointer; }
...
private:
ObjectType* Pointer;
};

Now my compiler can compile:

SmartPointer<Object> s = new Object;
delete s;

Is there a way to prevent that ?




是的。如果你没有明确地告诉编译器这是允许的,那么它就不会允许它。这意味着删除运算符ObjectType *()。



Yes. If you don''t explicitly tell the compiler that this is allowed, it
won''t allow it. That means remove the operator ObjectType*().


Daniel T.写道:
Daniel T. wrote:
智能指针<对象> s = new Object;
删除s;

有没有办法防止这种情况?
SmartPointer<Object> s = new Object;
delete s;

Is there a way to prevent that ?



是的。如果你没有明确地告诉编译器这是允许的,它就不会允许它。这意味着删除运算符ObjectType *()。



Yes. If you don''t explicitly tell the compiler that this is allowed, it
won''t allow it. That means remove the operator ObjectType*().




当然,修复是最好的风格。 (这就是为什么C ++标准库如果没有转换运算符那么几乎没有
。)


但是模板化的类也不能提供运算符删除?然后它可以正确地zilch它拥有指针。


-

Phlip
http://c2.com/cgi/wiki?ZeekLand < - 不是博客!! !



Of course that fix is the best style. (It''s why the C++ Standard Library has
few if no conversion operators.)

But can''t the templated class also provide an operator delete? Then it could
correctly zilch its owning pointer.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!




Daniel T.写道:

Daniel T. wrote:
是的。如果你没有明确地告诉编译器这是允许的,它就不会允许它。这意味着删除运算符ObjectType *()。
Yes. If you don''t explicitly tell the compiler that this is allowed, it
won''t allow it. That means remove the operator ObjectType*().




那么还有 - 至少 - 在

中定义~Object的另一种方式受保护的部分,但这样做会阻止我在堆栈上定义一个对象。

我以为可能有另一种方式...

-M



Well there is -at least- another way like defining the ~Object in the
protected section, but doing so would prevent me to define an Object on
the stack.
I thought there could be another way...

-M


这篇关于防止在SmartPointer上删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆