如何在向量中存储子 类 并覆盖父函数
How to store child classes in vector and override parent function
我正在尝试使用 C++ 和 SDL2 编写来自很棒的 YouTube 频道 "meth meth method" 的 js 超级马里奥系列。
现在我卡在第 04 集了 - https://www.youtube.com/watch?v=1rBOUyRGQuU
我正在尝试创建一个 Entity
class,它有一个 Trait
class 的向量。我想创建许多不同的 classes 继承自 Trait
并将它们存储在向量中。我想调用 subclasses 更新函数而不是原来的 Trait 更新函数。
// -- Entity.h -----------------------------------
class Entity {
private:
Vec2 vel;
std::vector<Trait*> traits;
public:
Vec2 pos;
// Constructor & Destructor
Entity(void);
~Entity(void);
void addTrait(Trait* nTrait);
void update();
};
// -- Entity.cpp -----------------------------------
void Entity::addTrait(Trait* newTrait) {
this->traits.push_back( newTrait );
}
void Entity::update() {
for ( Trait* trait : this->traits ) {
trait->update( );
}
}
// -- Trait.h -----------------------------------
class Trait {
private:
std::string name;
public:
// Constructor & Destructor
Trait(std::string aname );
~Trait(void);
void update( void );
};
// -- Trait.cpp -----------------------------------
void Trait::update( ) {
printf("Trait update\n");
}
还有一个来自 Trait
的 Velocity.h 子class
// -- Velocity.h -----------------------------------
class Velocity : public Trait {
private:
public:
// Constructor & Destructor
Velocity(std::string aname);
~Velocity(void);
void update(Entity *entity);
};
// -- Velocity.cpp -----------------------------------
void Velocity::update(Entity *entity ) {
printf("Velocity update\n");
entity->setPos(0, 0);
}
我尝试了很多不同的版本,得到了不同的错误。现在我开始工作了,当我向实体添加速度特征并调用实体的更新方法(循环遍历所有特征)时,只有父特征 class 调用更新函数(打印 "Trait Update" 而不是 "Velocity Update").
main.cpp
Entity *mario = new Entity();
mario->setPos( 64, 180 );
mario->setVel( 2, -2 );
Velocity* velTrait = new Velocity("velocity");
mario->addTrait(velTrait);
mario->update();
第二个问题是,我想为速度更新函数提供一个指向实体的指针,以便我可以更改实体位置等...
I want to create many different classes which inherit from Trait
and store them in the vector. I want to call the subclasses update function and not the original Trait
update function.
要得到你想要的,使用virtual
方法:
virtual void update( void );
更详细的建议你去研究多态和虚函数
我正在尝试使用 C++ 和 SDL2 编写来自很棒的 YouTube 频道 "meth meth method" 的 js 超级马里奥系列。
现在我卡在第 04 集了 - https://www.youtube.com/watch?v=1rBOUyRGQuU
我正在尝试创建一个 Entity
class,它有一个 Trait
class 的向量。我想创建许多不同的 classes 继承自 Trait
并将它们存储在向量中。我想调用 subclasses 更新函数而不是原来的 Trait 更新函数。
// -- Entity.h -----------------------------------
class Entity {
private:
Vec2 vel;
std::vector<Trait*> traits;
public:
Vec2 pos;
// Constructor & Destructor
Entity(void);
~Entity(void);
void addTrait(Trait* nTrait);
void update();
};
// -- Entity.cpp -----------------------------------
void Entity::addTrait(Trait* newTrait) {
this->traits.push_back( newTrait );
}
void Entity::update() {
for ( Trait* trait : this->traits ) {
trait->update( );
}
}
// -- Trait.h -----------------------------------
class Trait {
private:
std::string name;
public:
// Constructor & Destructor
Trait(std::string aname );
~Trait(void);
void update( void );
};
// -- Trait.cpp -----------------------------------
void Trait::update( ) {
printf("Trait update\n");
}
还有一个来自 Trait
的 Velocity.h 子class// -- Velocity.h -----------------------------------
class Velocity : public Trait {
private:
public:
// Constructor & Destructor
Velocity(std::string aname);
~Velocity(void);
void update(Entity *entity);
};
// -- Velocity.cpp -----------------------------------
void Velocity::update(Entity *entity ) {
printf("Velocity update\n");
entity->setPos(0, 0);
}
我尝试了很多不同的版本,得到了不同的错误。现在我开始工作了,当我向实体添加速度特征并调用实体的更新方法(循环遍历所有特征)时,只有父特征 class 调用更新函数(打印 "Trait Update" 而不是 "Velocity Update").
main.cpp
Entity *mario = new Entity();
mario->setPos( 64, 180 );
mario->setVel( 2, -2 );
Velocity* velTrait = new Velocity("velocity");
mario->addTrait(velTrait);
mario->update();
第二个问题是,我想为速度更新函数提供一个指向实体的指针,以便我可以更改实体位置等...
I want to create many different classes which inherit from
Trait
and store them in the vector. I want to call the subclasses update function and not the originalTrait
update function.
要得到你想要的,使用virtual
方法:
virtual void update( void );
更详细的建议你去研究多态和虚函数