Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ - Casting a base class shared_ptr to a derived class shared_ptr
    primarykey
    data
    text
    <p>I tried reading through some topics that might have had the answer I was looking for, but I didn't get an answer.</p> <p>In any case. I have a class that holds a vector of shared_ptr's to a base (interface) class: IInputDevice.</p> <p>Here is the member vector in manager class: CInputManager:</p> <pre><code>// In class: CInputManager private: std::vector&lt;std::tr1::shared_ptr&lt;IInputDevice&gt;&gt; m_vecpInputDevices; </code></pre> <p>I also have a few helper functions, that is meant to retrieve a specific input device (i.e: keyboard, mouse, etc). </p> <p>In this case, I have a keyboard "getter" that calls the inputmanager's GetDevice function, that returns the smart_ptr in the vector, that has the specific input type ID.</p> <pre><code>inline std::tr1::shared_ptr&lt;CKeyboard&gt; GetKeyboard() { return CInputManager::GetInstance().GetInputDevice(CInputType::Keyboard()); } </code></pre> <p>The function is incomplete, as I need a cast of some sort, to get a </p> <pre><code>smart_ptr&lt;CKeyboard&gt; </code></pre> <p>type.</p> <p>Here is the "get" function in CInputManager, that searches the vector for the specific device:</p> <pre><code>std::tr1::shared_ptr&lt;IInputDevice&gt; CInputManager::GetInputDevice(CInputType type) { for( std::vector&lt;std::tr1::shared_ptr&lt;IInputDevice&gt;&gt;::iterator pDeviceIter = m_vecpInputDevices.begin(); pDeviceIter != m_vecpInputDevices.end(); pDeviceIter++) { if( (*pDeviceIter)-&gt;GetInputType() == type ) return (*pDeviceIter); } return NULL; </code></pre> <p>}</p> <p>The CInputType class also has a overloaded == operator, to check only if the ID's match. Hence the</p> <pre><code>if( (*pDeviceIter)-&gt;GetInputType() == type ) return (*pDeviceIter); </code></pre> <p>So, I'd love to hear from you guys if you have a smart, simple cast/solution to this problem, as you can notice that I'd like helper functions to retrieve a specific device.</p> <p>Thanks, and regards, Oyvind</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload