开发者

C++ - Smart Pointers - Passing derived class shared pointer to base through template

I have the following and having difficulty resolving the error please help.

i have the following class as template definition somewhere.

template<class ConcreteHandlerType>
class SomeAcceptor: public ACE_Acceptor<ConcreteHandlerType, ACE_SOCK_Acceptor>

In some other file, i initialize this class in the constructor

class initialize {

    typedef SomeAcceptor<BaseClassSomeHandler> baseAcceptor_t;
    typedef SomeAcceptor<DerivedClassSomeHandler> derivedAcceptor_t;
    boost::shared_ptr<baseAcceptor_t;> mAcceptor;   
    boost::shared_ptr<derivedAcceptor_t;> mDerivedAcceptor;   

    bool HandleAcceptNotification(BaseClassSomeHandler& someHandler);

    initialize() : mAcceptor(0), mDerivedAcceptor(new DerivedAcceptor_t) {
        mAcceptor->SetAcceptNotificationDelegate(fastdelegate::MakeDelegate(this, &initialize::HandleAcceptNotification));
    }
}

Error i get is

error: no matching function for call to `boost::shared_ptr<SomeAcceptor<BaseClassSo开发者_StackOverflow中文版meHandler> >::shared_ptr(int)'common/lib/boost_1_39_0/boost/smart_ptr/shared_ptr.hpp:160: note: candidates are: boost::shared_ptr<SomeAcceptor<BaseClassSomeHandler> >::shared_ptr(const boost::shared_ptr<SomeAcceptor<BaseClassSomeHandler> >&)
common/lib/boost_1_39_0/boost/smart_ptr/shared_ptr.hpp:173: notboost::shared_ptr<T>::shared_ptr() [with T = SomeAcceptor<BaseClassSomeHandler>]

I also tried overloading the function with bool HandleAcceptNotification(DerivedClassSomeHandler& someHandler);

but because mAcceptor is of type SomeAcceptor BaseClassSomeHandler, i get this error, but to fix this.

I guess i need to cast it somehow, but how to do it?

i tried doing like below inside the constructor and it didn't work

    initialize() : mAcceptor(0), mDerivedAcceptor(new DerivedAcceptor_t) {

        mAcceptor = mDerivedAcceptor;   // Error here

        mAcceptor->SetAcceptNotificationDelegate(fastdelegate::MakeDelegate(this, &initialize::HandleAcceptNotification));
    }


From your code, it looks like you want mAcceptor to be assigned NULL (0), if that is the case you don't need to initialize it at all, as the default constructor will take care of that. But, since you call a function on that (NULL) pointer immediately, its not immediately clear exactly what you want to do.

If you want mAcceptor and mDerivedAcceptor to point to the same (shared) object and assuming DerivedClassSomeHandler is derived from BaseClassSomeHandler, this is a situation where you should use boost::shared_static_cast, as described here.

There's also some good information in this apparently related question.


The error is due to the mAcceptor(0) in

initialize() : mAcceptor(0), mDerivedAcceptor(new DerivedAcceptor_t) {
    mAcceptor->SetAcceptNotificationDelegate(fastdelegate::MakeDelegate(this, &initialize::HandleAcceptNotification));
}

The smart_ptr default constructor assigns the wrapped ptr to NULL, so leave out mAcceptor(0) from the initialization list.


boost::shared_ptr<SomeAcceptor<BaseClassSomeHandler> >::shared_ptr(int)

It's yelling at you that there's no constructor that accepts an int.

Just use: mAcceptor()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜