I am trying to create a scenegraph with a vector of Base class pointer with different type of derived classes and their own behaviours. Now here is my question:
Code:
class Base
{
public:
virtual void setParent(Base* pParent);
virtual void addChild(Base* pChild);
// for simplicity I wont implement all functions
private:
Base* m_pParent;
std::vector<Base*> m_pChildren;
};
classDerivedOne:publicBase
{
public:
// virtual function override
int custom(){return1337;}
}
classDerivedTwo:publicBase
{
public:
int customtwo(){return1337+1;}
}
main(xyz)
{
DerivedOne* d1 =newDerivedOne();
DerivedTwo d2 =newDerivedTwo();
d1->addChild(d2);
for(auto e : d1->getChildren())
{
std::cout <<static_cast<DerivedOne*>(e)->custom(); std::endl;// returns 1337 twice instead of once
}
}
Now my question is how do I realize the above? How can I call a specific function if the node is of a specific type? I want to create a scenegraph that can hold a variety of different node-types and some must not be rendered, so I am really curious to see if there is some way to realize this idea? The problem is that at the moment, if I cast a base to a derived class and call the function, the function is executed even when originally the class wasnt of that derived class.