c++ - 当我使用父类指针将它们添加到向量时,我不能使用子类特定的功能
问题描述
我正在尝试自己创建一个“外星入侵者”游戏。为了创建敌人和玩家,我创建了一个名为“实体”的类并创建了它的子类。比如 Player、shootingEnemy、IdleEnemy。编码时,我意识到将它们收集在一起vector<Entity>
将使我的碰撞检测功能更容易。
在互联网上搜索后,我了解到这称为“对象切片”,并复制对象的任何基本部分。
所以最终版本变成了这个。
int main()
{
int BoardWidth = 50;
int BoardLength = 30;
vector<Bullet> bullets;
vector<Entity*> SpaceShips;
setup(SpaceShips, BoardWidth, BoardLength);
double ElapsedTime = 0;
int PreviousRoundSec = 0;
int PreviousRoundQSec = 0;
DrawGame(BoardWidth, BoardLength, SpaceShips, bullets);
int IsGameOver = 0;
auto start = chrono::steady_clock::now();
while(!IsGameOver)
{
// Updates EverySecond
if ((int)(ElapsedTime / 1000) > PreviousRoundSec)
{
PreviousRoundSec = (int)(ElapsedTime / 1000);
}
// Updates every quarter of a second
if ((int)(ElapsedTime / 250) > PreviousRoundQSec)
{
PreviousRoundQSec = (int)(ElapsedTime / 250);
}
// To keep time
auto end = chrono::steady_clock::now();
ElapsedTime = chrono::duration_cast<chrono::milliseconds>(end - start).count();
}
if (IsGameOver == 1)
{
// conjualations
}
else if (IsGameOver == 2)
{
// GameOver
}
return 0;
}
但是,当我尝试使用某些特定于子类的函数时,我收到一个编译器错误,提示“CLASS“Entity”没有任何名为“shoot”的成员。
我正在尝试练习类和多态性,所以我什至不知道这是否有解决方案,因为编译器无法知道该向量的哪个元素属于哪个子类。
这也是我的课程标题页,以备不时之需。
class Entity
{
public:
int x;
int y;
int width;
int length;
int hp;
bool shooting;
public:
Entity(int x, int y, int width, int length, int hp, bool shooting): x(x), y(y), width(width), length(length), hp(hp), shooting(shooting) {}
};
class Bullet : public Entity
{
private:
char dir;
int ID;
public:
Bullet(int x, int y, char GivenDir, int GivenID) : Entity(x, y, 1, 1, 1, false) { dir = GivenDir; ID = GivenID; }
void Move();
void IfHit(vector<Entity>& SpaceShips);
void IfOut();
};
class Player : public Entity
{
private:
char action = 'a';
public:
Player(int x, int y, int hp) : Entity(x, y, 3, 2, hp, true) {}
void GetAction();
void Move();
void Shoot(vector<Bullet>& bullets);
bool IfHit(vector<Entity>& SpaceShips, vector<Bullet>& bullets);
};
class IdleEnemy : public Entity
{
public:
IdleEnemy(int x, int y, int hp) : Entity(x, y, 3, 2, hp, false){}
bool IfHit(Player* player, vector<Bullet> &bullets);
void Move(char HordDir);
};
class ShootingEnemy : public Entity
{
public:
ShootingEnemy(int x, int y, int hp) : Entity(x, y, 3, 2, hp, true) {}
void Shoot(vector<Bullet> &bullets);
bool IfHit(Player* player, vector<Bullet> &bullets);
void Move(char HordDir);
};
解决方案
您需要检查 C++ 中的运行时多态性。让我们看看你怎么能做到这一点。首先,你需要改变你的Entity
类接口。您需要添加虚拟或纯虚拟功能。我添加了纯虚函数;
class Entity
{
public:
int x;
int y;
int width;
int length;
int hp;
bool shooting;
public:
Entity(int x, int y, int width, int length, int hp, bool shooting) : x(x), y(y), width(width), length(length), hp(hp), shooting(shooting) {}
void virtual Move() = 0; // pure virtual function
void virtual IfHit() = 0; // pure virtual function
};
虚函数是可覆盖的函数。此外,它们有实现,但是当我们谈论纯虚函数时,它们只为类提供接口。您需要在派生类中覆盖该函数。当您实现派生类时,您需要这样做,
class Bullet : public Entity
{
private:
char dir;
int ID;
public:
Bullet(int x, int y, char GivenDir, int GivenID) : Entity(x, y, 1, 1, 1, false) { dir = GivenDir; ID = GivenID; }
void Move()override;
void IfHit();
void IfOut();
};
class Player : public Entity
{
private:
char action = 'a';
public:
Player(int x, int y, int hp) : Entity(x, y, 3, 2, hp, true) {}
void GetAction();
void Move();
void Shoot(vector<Bullet>& bullets);
void IfHit()override {//code};
};
class IdleEnemy : public Entity
{
public:
IdleEnemy(int x, int y, int hp) : Entity(x, y, 3, 2, hp, false) {}
void IfHit()override;
void Move()override;
};
class ShootingEnemy : public Entity
{
public:
ShootingEnemy(int x, int y, int hp) : Entity(x, y, 3, 2, hp, true) {}
void Shoot(vector<Bullet>& bullets);
void IfHit()override;
void Move()override;
};
这些函数既可以内联实现,也可以在源文件中实现。此外,这些函数还有一个重点是返回值、函数签名和名称必须相同,除非您不使用协变返回类型。
正如在派生类中看到的,一些函数并不常见。我知道你的问题我该如何使用它:) 正如评论中提到的你需要使用dynamic_cast
运算符的 temple。
int main()
{
Entity* ptr = new ShootingEnemy{ 1,2,4 };
ptr->Move();
ptr->IfHit();
if (auto SE = dynamic_cast<ShootingEnemy*>(ptr))
SE->Shoot(...);
}
dynamic_cast
运算符是运行时转换运算符。它将基类指针的类型转换为派生类。它被称为向下转换。此外,它检查基类指针是否指向目标派生类。如果dynamic_cast
操作以失败完成,则返回null
并if statement
变为失败。通过这种方式,您可以使用运行时多态性和类成员函数。
顺便说一句,尽可能避免对象切片。您正在丢失派生类属性。
为了更好地理解,请参阅类 dynamic_cast
推荐阅读
- python - 我怎样才能使 2D 列表的选择排序
- typescript - 打字稿泛型:从函数参数的类型推断类型?
- javascript - 从 API 对象呈现对象属性的问题
- google-apps-script - 无法为整个域安装编辑器插件
- java - 从游戏板上获取最后一步
- pari - PARI/GP 中的位数
- reactjs - 在带有 react-router 的 React 中,如何在离开复杂视图时保留一个人的所有视图选择,然后直接返回?
- git - 从本地分支推送到上游分支
- javascript - 使用 i18n 的参数中的日期
- html - 如何从居中的导航栏徽标中删除边距以给链接更多空间