Boost logo

Boost Users :

From: Andrew Holden (aholden_at_[hidden])
Date: 2007-12-19 10:34:20


STenyaK (Bruno Gonzalez) [stenyak_at_[hidden]] wrote:
> On 12/19/07, Andrew Holden <aholden_at_[hidden]> wrote:
>> typedef shared_ptr<MyClass> pMyClass;
>> void* MyClass::actionThread(void *objectPointer)
>> {
>> MyClass * _obj = static_cast<MyClass*>(objectPointer);
>> pMyClass obj = _obj->shared_from_this();
>> obj->action(false);
>> }
>>
>> void MyClass::action(bool threaded)
>> {
>> if (threaded)
>> {
>> pthread_t tid;
>> pthread_create(&tid, 0, actionThread, this)
>> } else {
>> //TODO: actually perform the action
>> }
>> }
>> ----------------------------
>>
>> This works because shared_from_this is specifically designed to allow
>> member functions of a class to recover the shared pointer to the
>> object.
>
> Just in case... actionThread is a static method of the class, since
> otherwise C++ won't allow me to get a pointer of it (and i dislike
> global functions). Does this fact have any implications in the last
> proposed code sample?

Not really. actionThread is still a member function of MyClass, which
means it has full access to MyClass's members (as long as it has access
to a MyClass object somehow), including private and protected members,
and protected members from base classes. _obj is a valid pointer to a
MyClass, so there is nothing wrong with actionThread calling _obj's
shared_from_this function.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net