CsPointer  1.0.1
CsPointer::CsEnableSharedFromThis< T > Class Template Reference

Base class which provides a CsSharedPointer for an existing object. More...

Inherits enable_shared_from_this< T >

Public Member Functions

CsSharedPointer< T > sharedFromThis ()
 
CsSharedPointer< const T > sharedFromThis () const
 

Protected Member Functions

 ~CsEnableSharedFromThis () = default
 
 CsEnableSharedFromThis () = default
 
 CsEnableSharedFromThis (const CsEnableSharedFromThis &)
 
CsEnableSharedFromThis & operator= (const CsEnableSharedFromThis &)
 

Detailed Description

template<typename T>
class CsPointer::CsEnableSharedFromThis< T >

CsEnableSharedFromThis is a base class which provides a mechanism for obtaining a CsSharedPointer to the current object.

This class supports retrieving a valid CsSharedPointer to this. In C++ "this" is a keyword which evaluates to a raw pointer whose value refers to the current object. If you construct a shared pointer by using the "this" raw pointer the shared pointer will assume responsibility and ownership for the current object. In most cases this will lead to undefined behavior.

To use this class your class must inherit from CsEnableSharedFromThis and pass the name of your class as a template parameter.

std::vector<CsSharedPointer<MyWidget>> widgetList;
class MyWidget : public CsEnableSharedFromThis<MyWidget>
{
public:
MyWidget::MyWidget() = default;
CsSharedPointer<MyWidget> getSharedPtr() {
return sharedFromThis();
}
};
void addWidgetToList(MyWidget *ptr) {
CsSharedPointer<MyWidget> tmp = ptr->getSharedPtr();
widgetList.append(tmp);
}

Constructor & Destructor Documentation

template<typename T >
CsEnableSharedFromThis::~CsEnableSharedFromThis ( )
protecteddefault

Virtual destructor which does nothing.

template<typename T >
CsEnableSharedFromThis::CsEnableSharedFromThis ( )
protecteddefault

Constructs an empty CsEnableSharedFromThis object.

template<typename T >
CsEnableSharedFromThis::CsEnableSharedFromThis ( const CsEnableSharedFromThis< T > &  )
inlineprotected

Copy constructs a new CsEnableSharedFromThis with a nullptr.

Member Function Documentation

template<typename T >
CsEnableSharedFromThis & CsEnableSharedFromThis::operator= ( const CsEnableSharedFromThis< T > &  )
inlineprotected

Copy assigns a new CsEnableSharedFromThis with a nullptr.

template<typename T >
CsSharedPointer< T > CsEnableSharedFromThis::sharedFromThis ( )
inline

Returns a shared pointer which points to the current object, otherwise this method returns a null pointer.

template<typename T >
CsSharedPointer< const T > CsEnableSharedFromThis::sharedFromThis ( ) const
inline

Equivalent to calling sharedFromThis(), used when the T is const.