![]() |
CloudBuilder C++ SDK
v3.0.0
Making social games is easy !
|
#include <CotCHelpers.h>
Public Member Functions | |
autoref (T *ref, bool takeOwnership=false) | |
autoref (const autoref &ref) | |
template<class U > | |
autoref (const autoref< U > &ref) | |
template<class U > | |
autoref< T > & | operator= (const autoref< U > &ref) |
autoref & | operator= (const autoref &ref) |
autoref & | operator<<= (T *eptr) |
T * | operator -> () const |
T & | operator * () const |
operator T * () const | |
bool | operator== (const autoref &ref) const |
T * | get () const |
This smart pointer can optionally be used to automate the Retain/Release work on CRefClass objects. Calling Autorelease(obj) will create an autoref<Type> which will hold the object and call Release on it when it goes out of scope. Building an autoref<Type>(obj) will Retain the object upon creation and Release it when it goes out of scope. - If you are going to be the owner of the object (case of a return value or new Object) and want to release it when it goes out of scope, use Autorelease(obj). - If you are not the owner of the object and just want to keep a reference (case of a parameter) use autoref<Type>(obj). Example: C *c = new C(); autoref<C> cRef = Autorelease(c); // Will call Release once when cRef goes out of scope, freeing the object
But, if you pass as a parameter, you will probably want this instead void func(C *param) { If you had done Autorelease(c), the object would be Released at the end of this function, destroying it... (and you are not the owner) autoref<C> myRef = autoref<C>(param); } The example above becomes as follows (basically call Autorelease with any 'new' object and that's it): class C: public CRefClass { autoref member; // Released automatically in destructor
C(autoref<A> a) { member = a; } autoref getMember() { return member; } void setMember(autoref<A> newMember) { member = newMember; } autoref makeA() { return Autorelease(new A); } };
autoref<C> c = Autorelease(new C); // Released at the end of the scope autoref a1 = c.getMember(); autoref a2 = c.makeA(); Note: the <<= operator can be used to start tacking a reference to a new object. These 2 lines are equivalent: myRef = Autorelease(new Object); myRef <<= new Object;