Question

I have a class:

class Impl1 : public POA_I1
{
   private :
          Impl2_var ob;
   public :
          Impl2_ptr get();
          {
             return ob;
          }

          void set(::Impl2_ptr ob)
          {
             this->ob = ob;
          }
};

I'm a litte bit confused about _ptr and _var. I read that

MyObject_var The _var version of the object reference type acts as a handle toproxy in much the same way as a _ptr reference but also adds memory management. Like all _var types, a _var reference takes care of deallocating its underlying instance (in this case, the proxy instance) when the reference goes out of scope. Both _ptr references and _var references allow the client to access operations onproxy instance

but I dont understand when to use each one and why. I mean, in my implementation = which one should I use, _var or _ptr and why? And is it legal to have a field of type _var in class and return _ptr in setter? I just don't get it at all.

Was it helpful?

Solution

As Johnny pointed out, it's all about memory ownership. If you assign an _ptr variable to an _var variable, when the _var variable goes out of scope the memory will be deleted and you better not use that _ptr variable.

In the case you provided, when you call set you are giving an object of type Impl1 ownership of the pointer. You can still use the one you have, you can call Impl1::get but since you gave the object ownership of that _ptr by calling set, if the object gets deleted so does the memory referenced by that pointer.

OTHER TIPS

The current IDL to C++ mapping is pretty difficult and confusing. As always, consult the Henning & Vinowski book.

In general, do this for object references:

  1. Always pass object references as _ptr types in function arguments.
  2. But always store them (e.g. member variables, local variables) in _var types.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top