Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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<BaseClassSomeHandler> >::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));
    }
like image 829
devgp Avatar asked Jun 26 '26 04:06

devgp


1 Answers

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.

like image 147
Chad Avatar answered Jun 28 '26 19:06

Chad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!