[英]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); 我也试过用bool HandleAcceptNotification(DerivedClassSomeHandler和someHandler)重载函数;
but because mAcceptor is of type SomeAcceptor BaseClassSomeHandler, i get this error, but to fix this. 但因为mAcceptor的类型是SomeAcceptor BaseClassSomeHandler,我得到这个错误,但要解决这个问题。
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. 从你的代码看,你看起来想要为mAcceptor
分配NULL(0),如果是这种情况你根本不需要初始化它,因为默认的构造函数会处理它。 But, since you call a function on that (NULL) pointer immediately, its not immediately clear exactly what you want to do. 但是,由于你立即调用该(NULL)指针上的函数,它不能立即清楚你想要做什么。
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 . 如果你想mAcceptor
和mDerivedAcceptor
指向同一个(共享)对象,并假设DerivedClassSomeHandler
源自BaseClassSomeHandler
,这是一个情况下,你应该使用boost::shared_static_cast
,描述在这里 。
There's also some good information in this apparently related question . 在这个明显相关的问题中也有一些很好的信息。
The error is due to the mAcceptor(0)
in 该错误是由于mAcceptor(0)
所致
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. smart_ptr默认构造函数将包装的ptr分配给NULL,因此从初始化列表中省略了mAcceptor(0)。
boost::shared_ptr<SomeAcceptor<BaseClassSomeHandler> >::shared_ptr(int)
It's yelling at you that there's no constructor that accepts an int . 你大吼大叫说没有接受int的构造函数。
Just use: mAcceptor() 只需使用: mAcceptor()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.