在c++的另一个类的构造函数中实例化一个类的对象
Instantiate an object of one class in the constructor of another class C++?
我正在用c++写一个基本的游戏,它有3个类:Game
类,Character
类和Item
类。
Game
类将具有游戏的所有逻辑,因此main
函数将简单地创建Game object
,调用其逻辑函数,游戏将执行其他所有操作。可以有不止一个玩家。
Character
类有一个指针向量,可以保存一个或多个item。一个字符可以有一个或多个项目
Item
类具有该项的所有属性和功能。
我被游戏结构设计困住了。有人建议我在创建Game object
时,同时创建Character object
,然后角色对象将创建包含道具和Item object
的指针向量。所以它喜欢当我调用constructor of the Game class
时,它会调用constructor of the Character class
,字符类的构造函数会自动调用constructor of the Item class
。
这很有意义,但是我不知道如何正确地实现它。
这就是我得到的这是我目前所看到的:
Game.cpp
Game::Game()
{
vector<Character*> characterVector; //to hold Characters
}
Game::startLogic()
{
string name;
Character character = new Character(name);
}
Character.cpp
Character::Character(string name)
{
m_name = name;
vector<Item*> itemVector;
}
Item.cpp
Item::Item()
{ //initialise all the attributes of an item
}
main.cpp
void main()
{
Game g;
g.startLogic();
}
所以我可以在游戏运行时创建一个角色(我仍然需要稍后将该角色推入characterVector中),但我不太确定如何为该角色创建道具。我的意思是我应该把实例化代码放在哪里?在startLogic函数中,在Game的构造函数中,还是在Character的构造函数中?
你的向量在错误的地方。您需要将它们作为类成员移动到类声明中,而不是作为构造函数中的局部变量。构造函数可以填充向量(但实际上,角色知道他们"出生"的道具是什么吗?游戏知道什么角色在游戏一开始就活着吗?),但不应该声明向量。
试试这个:
Game.h
#include <vector>
class Character;
class Game
{
public:
std::vector<Character*> characters;
Game();
~Game();
void startLogic();
};
Game.cpp
#include "Game.h"
#include "Character.h"
#include <memory>
Game::Game()
{
}
Game::~Game()
{
for (std::vector<Character*>::iterator i = characters.begin();
i != characters.end();
++i)
{
delete *i;
}
}
Game::startLogic()
{
...
// using auto_ptr as a safety catch in case of memory errors...
std::auto_ptr<Character> c(new Character("Joe Smoe"));
std::auto_ptr<Item> i(new Item);
c->items.push_back(i.get());
i.release();
characters.push_back(c.get());
c.release();
...
}
Character.h
#include <string>
#include <vector>
class Item;
class Character
{
public:
std::string name;
std::vector<Item*> items;
Character(std::string aName);
~Character();
};
Character.cpp
#include "Character.h"
#include "Item.h"
Character::Character(std::string aName)
: name(aName)
{
}
Character::~Character()
{
for (std::vector<Item*>::iterator i = items.begin();
i != items.end();
++i)
{
delete *i;
}
}
Item.h
class Item
{
public:
Item();
};
Item.cpp
#include "Item.h"
Item::Item()
{ //initialise all the attributes of an item
}
main.cpp
int main()
{
Game g;
g.startLogic();
return 0;
}
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 从多个源构造一个对象,包括一个对象向量
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- 检查哪个对象调用了另一个对象的对象方法
- C++ 如何在将新对象分配给另一个对象时创建新对象
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 包装一个对象并假装它是一个 int
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- 如何使用函数将一个对象的输入复制到另一个对象中
- 选择一个元素而不是一个对象的数组的原因
- 为什么我可以使用 memcpy 将一个对象变量复制到另一个对象变量
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 我重载了 << 和 = 运算符。为什么当我将一个对象分配给另一个对象并尝试打印它时,我会被打印出来?
- 打印对象的映射,其中另一个对象作为键
- 如何通过另一个对象中的命令正确地从一个对象返回数据
- 为什么将两个对象分配给另一个对象后,两个对象不一样?
- 绘制一个对象,比较模具缓冲区的两个不同值