I have problem with storing child class object to the vector. I have parent class
class IngameObject
{
protected:
bool clickable = false;
};
I have second class
class Character : public IngameObject
{
protected:
bool clickable = true;
};
Now, I am trying to create new instance of Character and stroe it to the vector. I have vector defined as
std::vector<IngameObject*> objVector;
Code for creating Characters and stroring them is
for(int i = 0; i < 1; i++)
{
int a = i*32;
Character *c = new Character(this->holderTextures["texture"], sf::IntRect(a, a, 32, 32), sf::Vector2f(2*a, 10.f));
std::cout << instanceof<Character>(c) << " TEST" <<std::endl;
this->objVector.emplace_back( c );
std::cout << instanceof<Character>(this->objVector.back()) << " TEST" <<std::endl;
}
In the first case, instance is Character. After emplace_back to the objVector is type of instance IngameObject and bool clickable is set to false. What I am doing wrong?
EDIT Instanceof is defined as
template<typename Base, typename T>
inline bool instanceof(const T*) {
return std::is_base_of<Base, T>::value;
}