Re: possibility to forbid use of "this"?

From: tom_usenet (tom_usenet_at_hotmail.com)
Date: 01/07/04


Date: Wed, 07 Jan 2004 14:23:07 +0000

On Wed, 7 Jan 2004 14:53:45 +0100, "Ernst Murnleitner"
<mur-spam@awite.de> wrote:

>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.

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/sp_techniques.html#from_this

Tom

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



Relevant Pages

  • Re: pass by Reference/value ???
    ... > void foo ... this would be called "passing by reference". ... foo receives a local copy of s and any changes that it makes to s only ... Since s is a pointer, a variable storing the address of s is a pointer to ...
    (comp.lang.cpp)
  • Re: basic question about references
    ... It expects a reference, ... not a pointer. ... void f; // a reference ... introduced for operator overloading because using ...
    (alt.comp.lang.learn.c-cpp)
  • Re: <OT> About pointer
    ... > void h{ ... > explicitly noting each place at which an underlying pointer mechanism ... passing "by reference" or passing a pointer ... > What does this program fragment print? ...
    (comp.lang.c)
  • Re: Passing arguements by reference
    ... but doSomething doesnt change p unless i use "ref"? ... reference, hence both p and q pointing to the same object, not to different ... don't use the ref keyword. ... void Execute() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: STL Vector - pass by reference?
    ... I had a function that could populate a vector and pass it back to the caller, but people pointed out this would create a "copy" of the vector and it may be better to pass by reference. ... void GetDeviceClasses(vector& guids) ... your version is using a pointer. ...
    (microsoft.public.vc.language)