简体   繁体   English

C ++ - 智能指针 - 通过模板将派生类共享指针传递给base

[英]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 . 如果你想mAcceptormDerivedAcceptor指向同一个(共享)对象,并假设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.

相关问题 C ++-传递智能指针派生类 - C++ - passing smart pointer derived class 通过指向基类C ++的指针访问派生模板类的未知模板类型的成员 - Access members of unkown template type of derived template class through pointer to base class C++ 使用派生的 class C++ 返回基 class 共享指针 - Return Base class shared pointer using derived class C++ 将数组指针从派生类传递到基类C ++ - Passing array pointer from derived class to base class C++ 通过基类指针C ++访问派生类的成员 - Access members of derived class through base class pointer C++ C ++将派生类作为基类模板参数传递 - C++ Passing a derived class as a base class template parameter C ++指向基类的指针尚未获得派生类成员函数,但使用派生创建了指针 - c++ pointers to base class has not got derived class member functions but pointer is created with derived 通过基本指针C ++设计模式删除派生类 - Delete Derived Class Through Base Pointer C++ Design Pattern C ++ | 通过指向基础的指针访问派生类的方法? - C++ | Access derived class' method through pointer-to-base? 将派生的 class 作为参数传递给方法,该参数是带有智能指针的基础 class - Passing derived class to method as argument which is base class with smart pointer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM