C ++ - 将基类shared_ptr转换为派生类shared_ptr

时间:2013-07-13 20:45:28

标签: c++ stl tr1

我尝试阅读一些可能有我想要的答案的主题,但我没有得到答案。

无论如何。我有一个类,它将shared_ptr的向量保存到基类(接口)类:IInputDevice。

以下是manager类中的成员向量:CInputManager:

// In class: CInputManager
private:
    std::vector<std::tr1::shared_ptr<IInputDevice>> m_vecpInputDevices;

我还有一些辅助函数,用于检索特定的输入设备(即:键盘,鼠标等)。

在这种情况下,我有一个键盘“getter”,它调用inputmanager的GetDevice函数,该函数返回向量中的smart_ptr,它具有特定的输入类型ID。

inline std::tr1::shared_ptr<CKeyboard> GetKeyboard()
{
    return CInputManager::GetInstance().GetInputDevice(CInputType::Keyboard());
}

该功能不完整,因为我需要某种演员表来获得

smart_ptr<CKeyboard> 

类型。

这是CInputManager中的“get”函数,它在向量中搜索特定设备:

std::tr1::shared_ptr<IInputDevice> CInputManager::GetInputDevice(CInputType type)
{
for( std::vector<std::tr1::shared_ptr<IInputDevice>>::iterator pDeviceIter = m_vecpInputDevices.begin();
    pDeviceIter != m_vecpInputDevices.end();
    pDeviceIter++)
{
    if( (*pDeviceIter)->GetInputType() == type )
        return (*pDeviceIter);
}

return NULL;

}

CInputType类也有一个重载的==运算符,只检查ID的匹配。因此

if( (*pDeviceIter)->GetInputType() == type )
        return (*pDeviceIter);

所以,如果你对这个问题有一个聪明,简单的演员/解决方案,我很乐意听到你们的意见,因为你们可以注意到我想要帮助函数来检索特定的设备。

谢谢,并且, 的Øyvind

0 个答案:

没有答案
相关问题