Boost logo

Boost :

From: Oleg Fedtchenko (BoostOleg_at_[hidden])
Date: 2004-10-29 06:48:13


> Peter Dimov wrote:
>
> it must go via an analogous mechanism to obtain
>
> WORD* from A*:
>
> // in A
> friend shared_ptr<WORD> GetData( shared_ptr<A> this_ )
> {
> return shared_ptr<WORD>(
> &this_->m_wData,
> boost::bind( &shared_ptr<A>::reset, this_ )
> );
> }
>
> // in B
> friend shared_ptr<WORD> GetData( shared_ptr<B> this_ )
> {
> shared_ptr<A> pa( &this_->a,
> boost::bind( &shared_ptr<B>::reset, this_ ) );
>
> return GetData( pa );
> }

Doesn't it bring C++ *polymorphism* to naught?
Is it possible to implement the next example using global
functions as specified above?

class D
{
private:
    WORD m_wData;
public:
    D(){ m_wData = 0;}

    virtual shared_ptr<WORD> GetData( shared_ptr<D> this_ )
    {
        return shared_ptr<WORD>(
             &m_wData,
             boost::bind( &shared_ptr<D>::reset, this_ )
             );
        );
    }
}

class E : public D
{
private:
    WORD m_wOtherData;
public:

    virtual shared_ptr<WORD> GetData( shared_ptr<D> this_ )
    {
        return shared_ptr<WORD>(
             &m_wOtherData,
             boost::bind( &shared_ptr<D>::reset, this_ )
             );
        );
    }
}

// usage

void SomeFunc( shared_ptr<D> ptrObj)
{
    shared_ptr<WORD> ptrData = ptrObj->GetData( ptrObj);

    *ptrData = 57; // do the same for all objects
}
int main()
{
    shared_ptr<D> ptrD(new D);
    shared_ptr<E> ptrD(new E);

    SomeFunc( ptrD);
    SomeFunc( ptrE);
}

Oleg Fedtchenko

The proposed smart pointer member_ptr is described at
http://boostoleg.narod.ru/member_ptr.html

thread entry
http://lists.boost.org/MailArchives/boost/msg72943.php


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk