删除智能指针上的运算符。 [英] delete operator on smart pointers.

查看:98
本文介绍了删除智能指针上的运算符。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现我的自定义智能指针:


模板< typename T>

类MySmartPtr

{

public:

MySmartPtr(T * aPointer)

{

mPointer = aPointer;

}


内联T * operator->()const {return mPointer; }


私人:

T * mPointer;

};

它可用于这样:


MySmartPtr< Aobj = new A();

obj-> Method1();

int value = obj-> Method2();


运算符重载允许访问A方法,因为它们是通过公共指针访问的



我想允许最终用户调用删除操作符,就像在常见的

指针中,当他想要显式删除指针时:


删除obj;

有一种方法可以实现删除指针的重载

我的MySmartPtr< Tor有一些解决方法吗?


在此先感谢,

ernesto

I am implementing my custom smart pointer:

template <typename T>
class MySmartPtr
{
public:
MySmartPtr(T* aPointer)
{
mPointer = aPointer;
}

inline T* operator->() const {return mPointer; }

private:
T* mPointer;
};
It could be used in this way:

MySmartPtr<Aobj = new A();
obj->Method1();
int value = obj->Method2();

The operator-overload allows accessing the A methods as they are
accessed via a common pointer.

I want to allow the final user call the delete operator as in a common
pointer when he wants to explicitly remove the pointer:

delete obj;
There is a way I could implement an overload of the delete pointer in
my MySmartPtr<Tor there is some workaround on this?

Thanks in advance,
ernesto

推荐答案

" ErnestoBascón < eb ****** @ gmail.comschrieb im Newsbeitrag

news:11 ********************* @ s80g2000cwa。 googlegro ups.com ...
"Ernesto Bascón" <eb******@gmail.comschrieb im Newsbeitrag
news:11*********************@s80g2000cwa.googlegro ups.com...

>我正在实现我的自定义智能指针:


模板< typename T> ;

class MySmartPtr

{

public:

MySmartPtr(T * aPointer)

{

mPointer = aPointer;

}


内联T * operator->()const {return mPointer; }


私人:

T * mPointer;

};


它可以这样使用:


MySmartPtr< Aobj = new A();

obj-> Method1();

int value = obj-> Method2();


运算符重载允许访问A方法,因为它们是通过公共指针访问的



我想允许最终用户调用delete操作符,就像在一个常见的

指针中,当他想要显式删除指针时:


删除obj;


有一种方法可以实现删除指针的重载

我的MySmartPtr< Tor那里这是一些解决方法吗?
>I am implementing my custom smart pointer:

template <typename T>
class MySmartPtr
{
public:
MySmartPtr(T* aPointer)
{
mPointer = aPointer;
}

inline T* operator->() const {return mPointer; }

private:
T* mPointer;
};
It could be used in this way:

MySmartPtr<Aobj = new A();
obj->Method1();
int value = obj->Method2();

The operator-overload allows accessing the A methods as they are
accessed via a common pointer.

I want to allow the final user call the delete operator as in a common
pointer when he wants to explicitly remove the pointer:

delete obj;
There is a way I could implement an overload of the delete pointer in
my MySmartPtr<Tor there is some workaround on this?



为了使你的智能指针非常智能,它还应该实现一个默认的

构造函数,一个复制构造函数和一个赋值运算符(实际上是两个) ,一个

复制赋值运算符和一个指定原始指针的运算符。如果他们正确实施了
,你可以写一下


obj = 0; //或NULL


来释放之前拥有的smartpointer。


HTH

Heinz

To make your smart pointer really smart, it should also implement a default
constructor, a copy constructor and an assignment operator (actually two, a
copy assignment operator and one to assign raw pointers). If they are
implemented correctly, you could just write

obj = 0; // or NULL

to free whatever the smartpointer owned before.

HTH
Heinz




ErnestoBascón写道:

Ernesto Bascón wrote:

我正在实现我的自定义智能指针:


模板< typename T>

class MySmartPtr

{

public:

MySmartPtr(T * aPointer)

{

mPointer = aPointer;

}


内联T * operator->()const {return mPointer; }


私人:

T * mPointer;

};


它可以这样使用:


MySmartPtr< Aobj = new A();

obj-> Method1();

int value = obj-> Method2();


运算符重载允许访问A方法,因为它们是通过公共指针访问的



我想允许最终用户调用delete操作符,就像在一个常见的

指针中,当他想要显式删除指针时:


删除obj;


有一种方法可以实现删除指针的重载

我的MySmartPtr< Tor那里这是一些解决方法吗?


提前致谢,

ernesto
I am implementing my custom smart pointer:

template <typename T>
class MySmartPtr
{
public:
MySmartPtr(T* aPointer)
{
mPointer = aPointer;
}

inline T* operator->() const {return mPointer; }

private:
T* mPointer;
};
It could be used in this way:

MySmartPtr<Aobj = new A();
obj->Method1();
int value = obj->Method2();

The operator-overload allows accessing the A methods as they are
accessed via a common pointer.

I want to allow the final user call the delete operator as in a common
pointer when he wants to explicitly remove the pointer:

delete obj;
There is a way I could implement an overload of the delete pointer in
my MySmartPtr<Tor there is some workaround on this?

Thanks in advance,
ernesto



你试过debuggig并观察实际使用的对象然后

直接删除它吗?

did you try debuggig and watch what object is actually in use then
delete it directly?




Heinz Ozwirk写道:

Heinz Ozwirk wrote:

>

为了使你的智能指针非常智能,它还应该实现一个默认的

构造函数,一个复制构造函数和一个赋值运算符(实际上是两个,一个

复制赋值运算符和一个指定原始指针的运算符)。如果他们正确实施了
,你可以写一下


obj = 0; //或NULL


免费获得之前拥有的smartpointer。


HTH

Heinz
>
To make your smart pointer really smart, it should also implement a default
constructor, a copy constructor and an assignment operator (actually two, a
copy assignment operator and one to assign raw pointers). If they are
implemented correctly, you could just write

obj = 0; // or NULL

to free whatever the smartpointer owned before.

HTH
Heinz



这是一个优雅的观点!

我用我的眼镜仔细观察了我的谷歌搜索,我转了
与他人有同样错误的想法。呵呵

That''s an elegant point!
I watched my google search carefully with my pair of glasses and I
turned out to have the same incorrect thought with others. hehe


这篇关于删除智能指针上的运算符。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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