I'm currently trying to find a list of derived classes in a list of unique_ptr<BaseClass>(Ptr from now on). Problem is, it can't convert the derived class into a Ptr. Here's the code.
Ptr declaration
1 2 3 4 5
class cSceneNode
{
typedef std::unique_ptr<cSceneNode> Ptr;
std::vector<Ptr> mChildren;
}
Derived class
1 2 3 4 5 6
class cEntity
:public cSceneNode
{
public:
void move(sf::Vector2f Pos);
}
Void move, A.K.A. derived members of mChildren
1 2 3 4 5 6 7 8 9 10
void cEntity::move(sf::Vector2f Pos)
{
mSprite.move(Pos);
for each(cEntity* _child in mChildren)
{
_child->move(Pos);
}
}
Now, here's the thing. Back when mChildren was just a vector of raw pointers (Yeah, I know, bad practice), I could easily access the cEntity's in the vector. I really don't want to use casting, but if there's some other way, please let me know. Thanks.
I'm honestly not really sure what to do about this other than stop using smart pointers, but if I do that, using a vector of raw pointers is going to be a pain to allocate. I've tried Googling my problems, but from what I've seen, I'm literally the only person who has ever thought about iterating over a derived type in a list of a base type.
EDIT:
Should be noted that as of the 2015 Vocabulary Reformation Act, literally now means figuratively.