禁止使用“this”的可能性? [英] possibility to forbid use of "this"?

查看:74
本文介绍了禁止使用“this”的可能性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的读者,


是否可以禁止转换或使用此一般情况

除非明确要求?


原因:


我将程序从使用普通指针更改为A类......


typedef A * APtr;


共享指针


typedef boost :: shared_ptr< A> APtr;


现在,因为这样的陈述而崩溃


//致电

DoSomething(this) ;


.....

//实施

无效DoSomething(APtr a)

{

//什么都不做

}


显然,这个在本地转换为共享ptr。函数外部

DoSomething()共享ptr被销毁,因此它试图删除

类,其中this点也是。这显然不是必需的。


问候,

非常感谢提前


恩斯特

Dear Readers,

Is it possible to forbid conversion from this or use of this in general
except where it is explicitly wanted?

Reason:

I changed my program from using normal pointers to classes A, ...

typedef A * APtr;

to a shared pointer

typedef boost::shared_ptr<A> APtr;

Now, it crashes because of statements like this

// call
DoSomething(this);

.....
// implementation
void DoSomething(APtr a)
{
// do nothing with a
}

Obviously, "this" is converted to a shared ptr locally. Outside the function
DoSomething() the shared ptr is destroyed and hence it tries to delete the
class where "this" points, too. This is clearly not wanted.

Greetings,
Many thanks in advance

Ernst

推荐答案

2004年1月7日星期三14:53:45 +0100,Ernst Murnleitner

< mu ******@awite.de>写道:
On Wed, 7 Jan 2004 14:53:45 +0100, "Ernst Murnleitner"
<mu******@awite.de> wrote:
亲爱的读者,

除非明确要求,否则是否可以禁止从此转换或使用此转换?

原因:

我将程序从使用普通指针改为A类......

typedef A * APtr;

共享指针

typedef boost :: shared_ptr< A> APtr;

现在,因为这样的陈述而崩溃

//致电
DoSomething(this);

... 。
//实现
void DoSomething(APtr a)
{
//什么都不做
}


那不应该编译 - 带有T *的shared_ptr的构造函数是明确的
,所以指针不能隐式转换为shared_ptr。

显然, ;这"在本地转换为共享ptr。在函数之外
DoSomething()共享ptr被破坏,因此它试图删除
类,其中this点也是。这显然不是必需的。
Dear Readers,

Is it possible to forbid conversion from this or use of this in general
except where it is explicitly wanted?

Reason:

I changed my program from using normal pointers to classes A, ...

typedef A * APtr;

to a shared pointer

typedef boost::shared_ptr<A> APtr;

Now, it crashes because of statements like this

// call
DoSomething(this);

....
// implementation
void DoSomething(APtr a)
{
// do nothing with a
}
That shouldn''t compile - the constructor of shared_ptr taking a T* is
explicit, so a pointer can''t implicitly convert to a shared_ptr.
Obviously, "this" is converted to a shared ptr locally. Outside the function
DoSomething() the shared ptr is destroyed and hence it tries to delete the
class where "this" points, too. This is clearly not wanted.




您有几个选择,具体取决于DoSomething的功能。如果

DoSomething没有持有对a的引用,那么你可以改变它

到:


void DoSomething (A& aref)

{

}


如果它确实持有对a的引用,但你知道它会释放

在销毁之前的参考(你可能不能保证
保证),然后你可以这样做:


struct null_deleter

{

void operator()(void const *)const

{

}

};


// ...


DoSomething(APtr(this,null_deleter()));


安全的方法是从this指针创建一个shared_ptr。这个

只有在A / $
第一名的A对象被创建为shared_ptr时才有可能。按照此处的说明进行操作:
http:// www .boost.org / libs / smart_ptr / ... html#from_this


Tom


C ++ FAQ: http://www.parashift.com/c++-faq-lite/

C常见问题解答: http: //www.eskimo.com/~scs/C-faq/top.html



You have a few choices, depending on what DoSomething does. If
DoSomething doesn''t hold onto a reference to a, then you can change it
to:

void DoSomething(A& aref)
{
}

If it does hold onto a reference to a, but you know it will release
that reference before a is destroyed (which you probably can''t
guarantee), then you could do:

struct null_deleter
{
void operator()(void const *) const
{
}
};

//...

DoSomething(APtr(this, null_deleter()));

The safe approach is to make a shared_ptr from the this pointer. This
is only possible if the A object was created as a shared_ptr in the
first place. Follow the instructions here:
http://www.boost.org/libs/smart_ptr/...html#from_this

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html




" Ernst Murnleitner" <亩****** @ awite.de>在消息新闻中写道:bt ************ @ ID-130107.news.uni-berlin.de ...

"Ernst Murnleitner" <mu******@awite.de> wrote in message news:bt************@ID-130107.news.uni-berlin.de...
显然,这个在本地转换为共享ptr。在函数外部
Obviously, "this" is converted to a shared ptr locally. Outside the function


DoSomething()共享ptr被破坏,因此它试图删除
类,其中this是点也是。这显然不是必需的。


DoSomething() the shared ptr is destroyed and hence it tries to delete the
class where "this" points, too. This is clearly not wanted.




shared_pointer(T *)是不是声明了?似乎它应该是。



shared_pointer(T*) isn''t declared explicit? Seems like it ought to be.


>这不应该编译 - shared_ptr的构造函数采用T *是
> That shouldn''t compile - the constructor of shared_ptr taking a T* is
explicit,所以指针不能隐式转换为shared_ptr。
explicit, so a pointer can''t implicitly convert to a shared_ptr.



使用gcc 2.95编译:


// ------------------------- ------------

#include< iostream.h>


#include< stdlib.h>


#include< boost / shared_ptr.hpp>


A级;


typedef boost: :shared_ptr的< a取代; APtr;


A级{


公开:


A(){};


虚拟~A(){};


虚拟空虚f(){func(this); };


void func(APtr p){cout<< " FUNC(APTR)" << std :: endl;};


};


int main(int argc,char * argv [])
< br $>
{


A a;


af();


返回EXIT_SUCCESS;


}


// ------------------- ------------------



With gcc 2.95 it compiles:

// -------------------------------------
#include <iostream.h>

#include <stdlib.h>

#include <boost/shared_ptr.hpp>

class A;

typedef boost::shared_ptr<A> APtr;

class A {

public:

A(){};

virtual ~A(){};

virtual void f(){func(this); };

void func(APtr p) {cout << "func(APtr)" << std::endl;};

};

int main(int argc, char *argv[])

{

A a;

a.f();

return EXIT_SUCCESS;

}

// -------------------------------------


这篇关于禁止使用“this”的可能性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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