vector<warrior> war_red;warrior* p;p = new dragon(1, hp[n], num, atk[n]); p->show(); war_red.push_back(*p);为什么vector内部是空的,要如何修改这段代码
时间: 2024-02-12 17:07:40 浏览: 32
这段代码中创建了一个 dragon 对象,并将其加入到 war_red 向量中,但是 war_red.push_back() 接收的是对象的副本,而不是指针或引用。因此,这段代码中创建的 dragon 对象在添加到 war_red 向量中后,它的生命周期已经结束,而向量中只剩下了它的一个副本。
要解决这个问题,可以将 war_red 向量中的元素类型改为指针类型,如下所示:
```cpp
vector<warrior*> war_red;
warrior* p = new dragon(1, hp[n], num, atk[n]);
p->show();
war_red.push_back(p);
```
这样就可以将创建的 dragon 对象的指针加入到 war_red 向量中,而不是将对象的副本加入到向量中。注意,在修改后的代码中,需要手动释放创建的 dragon 对象的内存,以防止内存泄漏。可以在程序结束前,循环遍历 war_red 向量,释放其中的每个对象的内存。
相关问题
class headquarter : public city { protected: game_controller* _controller; camp_label _camp; int _health_point; std::vector<std::unique_ptr<warrior>> _warriors; // RAII int _last_generate_warrior = -1; std::array<int, warrior_type_count> _warrior_record{ 0 }; bool _stopped = false; public: headquarter(camp_label camp, int health_point, game_controller* controller) noexcept : _camp(camp), _health_point(health_point), _controller(controller) {} virtual ~headquarter() = default; virtual void on_update_time(int new_time) override; protected: void generate_warrior(int time); };
这是一个 C++ 的类 headquarter,它继承自另一个类 city。headquarter 类具有一些成员变量和成员函数:
- _controller:指向游戏控制器的指针。
- _camp:阵营标签,用于标识是红方还是蓝方。
- _health_point:总血量。
- _warriors:一个 vector,用来存储生成的 warrior 对象的智能指针,这里使用了 RAII(资源获取即初始化)技术,确保对象的正确释放。
- _last_generate_warrior:上一次生成 warrior 的时间。
- _warrior_record:一个数组,用来记录不同类型 warrior 的数量,长度为 warrior_type_count。
- _stopped:标志 headquarter 是否已经停止生成 warrior。
成员函数:
- 构造函数 headquarter:初始化 _camp、_health_point 和 _controller。
- 析构函数 ~headquarter():默认析构函数。
- on_update_time:虚函数,用来在时间更新时更新 headquarter 对象的状态。
- generate_warrior:生成 warrior 对象,并将其存储在 _warriors 中。
std::vector<std::vector<Warrior*>> myArray(5, std::vector<Warrior*>(5));warrior是一个基类,数组中每个元素为其派生类,如何不使用typeid确定他是哪种派生类
如果你想要在不使用`typeid`的情况下确定一个指向基类的指针所指向的对象的派生类类型,你可以使用虚函数。具体来说,你可以在基类中定义一个虚函数,然后在每个派生类中覆盖这个虚函数,以便在运行时获取对象的派生类类型。
例如,你可以在`Warrior`类中定义一个虚函数`getType()`,然后在每个派生类中覆盖这个函数,返回该派生类的类型。具体的实现如下:
```
#include <iostream>
#include <vector>
class Warrior {
public:
virtual std::string getType() const { return "Warrior"; }
};
class DerivedClass1 : public Warrior {
public:
std::string getType() const override { return "DerivedClass1"; }
};
class DerivedClass2 : public Warrior {
public:
std::string getType() const override { return "DerivedClass2"; }
};
int main() {
std::vector<std::vector<Warrior*>> myArray(2, std::vector<Warrior*>(2));
myArray[0][0] = new DerivedClass1();
myArray[0][1] = new DerivedClass2();
myArray[1][0] = new DerivedClass2();
myArray[1][1] = new DerivedClass1();
// 遍历数组中的每个元素,输出其类型
for (auto& row : myArray) {
for (auto& warrior : row) {
std::cout << warrior->getType() << " ";
}
std::cout << std::endl;
}
return 0;
}
```
在这个例子中,`Warrior`类中定义了一个虚函数`getType()`,返回字符串"Warrior"。`DerivedClass1`和`DerivedClass2`中都覆盖了这个函数,返回它们自己的类名。
在`main()`函数中,我们创建了一个二维vector,其中每个元素都是`Warrior`类的派生类。然后,我们遍历数组中的每个元素,并调用其`getType()`函数来获取其派生类类型,并输出到控制台上。
这样,你就可以在运行时获取对象的派生类类型,而不需要使用`typeid`操作符。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)