Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-05-08 07:28:29


Jean-François Brouillet wrote:

> So, I added a copy constructor:
>
> template <typename T>
> struct Bridge : public boost::shared_ptr<T> {
>
> template <typename Y>
> explicit Bridge(Y * y) : boost::shared_ptr<Y>(y) {
> if (y) {
> y->javaConstructor() ;
> }
> }
>
> template<typename Y>
> Bridge(Bridge<Y> const & r) : boost::shared_ptr<Y>(r) {
> }
> } ;
>
> But then I get:
>
> /Users/verec/Tools/boost_1_32_0/osx/main.cpp:21: error: type 'class
> boost::shared_ptr<DisplayStruct>' is not a direct base of
> 'Bridge<DeviceStruct>'

That's because shared_ptr<Y> is not a base of Bridge<T>, shared_ptr<T> is.
Change the templated copy constructor to:

template<typename Y>
    Bridge(Bridge<Y> const & r) : boost::shared_ptr<T>(r) {
}

and it will compile.

You can also use a static create function:

class Display
{
public:

    typedef shared_ptr<Display> ref;

    static ref create()
    {
        ref r( new Display );
        r->constructor();
        return r;
    }

private:

    Display() { ... }
};

int main()
{
    Display::ref d = Display::create();
}


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