[aspectc-user] reset tjp->that()?

Yan Mao maoyan at gmx.net
Sat Dec 9 17:18:10 CET 2006


Hello Daniel,

thank for the explation, it's very helpful.

regards,

Yan Mao



-------- Original-Nachricht --------
Datum:  Fri, 08 Dec 2006 17:06:02 +0100
Von: Daniel Lohmann <daniel.lohmann at informatik.uni-erlangen.de>
An: aspectc-user at aspectc.org
Betreff:  Re: [aspectc-user] reset tjp->that()?

> Hi Yan,
> 
> I am afraid what you are trying to achieve is not possible.
> 
> > //the C++ code
> > ClassA *p = new ClassA();
> > p->DoSomething();
> > p->Release();
> > //now i want to set p=NULL with AOP
> 
> In C++ the object instance pointer (this) is always passed by-value.
> Technically, it is passed as a "hidden" first parameter:
> 
> class A {
>    void Release();
> };
> 
> becomes (technically) something such as
> 
> void ClassA::Release( ClassA* this ) {
> 	// this is by-value parameter
> }
> 
> The object reference inside some method (this) is a copy of the original
> parameter. The same holds for the value returned by tjp->that()   in
> advice code, it is another value copy of the originally passed instance
> pointer. It is not possible to affect the "original" from advice code.
> 
> Side node:
> The common C++ idiom to invalidate the reference when destroying an
> instance is to use a non-member function:
> 
> // Template, works for any class that offers a Release() method
> template< class T > void Release( T*& _this ) {
> 	_this->Release();	// destroy
> 	_this = 0;		// invalidate
> }
> 
> //the C++ code
> ClassA *p = new ClassA();
> p->DoSomething();
> Release(p);
> // now p == 0
> 
> 
> Daniel

-- 
"Ein Herz für Kinder" - Ihre Spende hilft! Aktion: www.deutschlandsegelt.de
Unser Dankeschön: Ihr Name auf dem Segel der 1. deutschen America's Cup-Yacht!



More information about the aspectc-user mailing list