将派生 class 存储到向量 C++
Storing derived class to the vector C++
我在将 child class object 存储到向量时遇到问题。我有 parent class
class IngameObject
{
protected:
bool clickable = false;
};
我有第二个class
class Character : public IngameObject
{
protected:
bool clickable = true;
};
现在,我正在尝试创建 Character 的新实例并将其存储到向量中。我将矢量定义为
std::vector<IngameObject*> objVector;
创建角色并存储它们的代码是
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;
}
第一种情况,instance是Character。在 emplace_back 之后,objVector 是实例 IngameObject 的类型,并且 bool clickable 设置为 false。我做错了什么?
编辑
Instanceof 定义为
template<typename Base, typename T>
inline bool instanceof(const T*) {
return std::is_base_of<Base, T>::value;
}
将派生的 class 存储在 std::vector
中是正确的:问题与多态性或“将派生的 class 存储在向量中”无关:
#include <iostream>
#include <vector>
class IngameObject
{
protected:
bool clickable = false;
};
class Character : public IngameObject
{
protected:
bool clickable = true;
};
int main()
{
std::vector<IngameObject*> objVector;
Character *c = new Character();
objVector.emplace_back( c );
delete c;
return 0;
}
问题与你实现有关instanceof
要解决 instanceof
函数,您的 class 需要是多态的。然后,指向基class的指针可以转换为派生class,从而确认它是instanceof
基class:
#include <iostream>
#include <vector>
class IngameObject
{
protected:
bool clickable = false;
virtual ~IngameObject() = default;
};
class Character : public IngameObject
{
protected:
bool clickable = true;
};
template<class T, class U>
inline bool instanceof(U* ptr) {
return nullptr != dynamic_cast<T*>(ptr);
}
int main()
{
std::vector<IngameObject*> objVector;
Character *c = new Character();
std::cout << instanceof<Character>(c) << " TEST" <<std::endl;
objVector.emplace_back( c );
std::cout << instanceof<Character>(objVector.back()) << " TEST" <<std::endl;
delete c;
return 0;
}
注意:要使Base
多态,它需要至少有一个虚方法。
我在将 child class object 存储到向量时遇到问题。我有 parent class
class IngameObject
{
protected:
bool clickable = false;
};
我有第二个class
class Character : public IngameObject
{
protected:
bool clickable = true;
};
现在,我正在尝试创建 Character 的新实例并将其存储到向量中。我将矢量定义为
std::vector<IngameObject*> objVector;
创建角色并存储它们的代码是
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;
}
第一种情况,instance是Character。在 emplace_back 之后,objVector 是实例 IngameObject 的类型,并且 bool clickable 设置为 false。我做错了什么?
编辑 Instanceof 定义为
template<typename Base, typename T>
inline bool instanceof(const T*) {
return std::is_base_of<Base, T>::value;
}
将派生的 class 存储在 std::vector
中是正确的:问题与多态性或“将派生的 class 存储在向量中”无关:
#include <iostream>
#include <vector>
class IngameObject
{
protected:
bool clickable = false;
};
class Character : public IngameObject
{
protected:
bool clickable = true;
};
int main()
{
std::vector<IngameObject*> objVector;
Character *c = new Character();
objVector.emplace_back( c );
delete c;
return 0;
}
问题与你实现有关instanceof
要解决 instanceof
函数,您的 class 需要是多态的。然后,指向基class的指针可以转换为派生class,从而确认它是instanceof
基class:
#include <iostream>
#include <vector>
class IngameObject
{
protected:
bool clickable = false;
virtual ~IngameObject() = default;
};
class Character : public IngameObject
{
protected:
bool clickable = true;
};
template<class T, class U>
inline bool instanceof(U* ptr) {
return nullptr != dynamic_cast<T*>(ptr);
}
int main()
{
std::vector<IngameObject*> objVector;
Character *c = new Character();
std::cout << instanceof<Character>(c) << " TEST" <<std::endl;
objVector.emplace_back( c );
std::cout << instanceof<Character>(objVector.back()) << " TEST" <<std::endl;
delete c;
return 0;
}
注意:要使Base
多态,它需要至少有一个虚方法。