保存类指针对象值的变量
Variable that holds value of class pointer object?
我正在尝试创建一个变量来保存类指针对象。
下面是我的两个具有构造函数值的类指针对象。
Goblin *g = new Goblin(6, 6, 3, 8, 2,1);
Barbarian *b = new Barbarian(6, 6, 0, 12, 2, 2);
我想创建一个变量来保存这样的数据,这样我就可以让玩家选择他想成为的生物,这个变量会保存这些数据,所以我可以将其用于我的功能。
这是我要使用的函数的标题。
void battle(Creature *a, Creature *d, std::string nameA, std::string nameD);
这是在main()中调用的函数
battle(g, b, playerName, opponentCreature);
值g和b是我想要用新变量替换的值。我尝试过使用字符串和int变量类型,但没有成功。有人知道我该怎么做吗?
我通过使用这两行让它开始工作。
Creature *personCode = 0;
Creature *opponentCode = 0;
以下是它们与我的程序的联系方式。
std::cout << "Choose your creature." << std::endl;
std::cout << "n1). Goblin." << std::endl;
std::cout << "2). Barbarian." << std::endl;
std::cin >> choice;
std::cin.clear();//clear the input
std::cin.ignore(100, 'n');//ignore 100 char or up to new line.
switch (choice)
{
case 1:// std::cout << "Goblin" << std::endl;
playerCreature = "Goblin";
personCode = new Goblin(6, 6, 3, 8, 2, 1);
break;
case 2:// std::cout << "Barbarian" << std::endl;
playerCreature = "Barbarian";
personCode = new Barbarian(6, 6, 0, 12, 2, 2);
break;
}
std::cout << "Choose your opponent" << std::endl;
std::cout << "n1). Goblin." << std::endl;
std::cout << "2). Barbarian." << std::endl;
std::cin >> choice;
std::cin.clear();//clear the input
std::cin.ignore(100, 'n');//ignore 100 char or up to new line.
switch (choice)
{
case 1: //std::cout << "Goblin" << std::endl;
opponentCreature = "Goblin";
opponentCode = new Goblin(6, 6, 3, 8, 2, 1);
break;
case 2: //std::cout << "Barbarian" << std::endl;
opponentCreature = "Barbarian";
opponentCode = new Barbarian(6, 6, 0, 12, 2, 2);
break;
}
这是现在调用的函数。
battle(personCode, opponentCode, playerName, opponentCreature);
相关文章:
- 你能重载对象变量名本身返回的内容吗
- 将包含C样式数组的对象初始化为成员变量(C++)
- 将地址分配给本地指针后,公共对象的变量将消失
- 为什么我们再次从结构对象创建结构变量?
- 如何在初始化列表中的构造函数之后初始化变量/对象?
- 从变量对象指向基对象
- C++ - 我可以创建编译时变量对象吗?
- 动态数组添加元素而不创建结构变量/对象
- Visual Studio C++ 编译器在局部变量对象上的奇怪行为
- 其中定义函数的静态局部变量对象
- QT良好的全局变量/对象处理
- r 值的匿名变量对象与真实对象
- QT 测试 - 变量/对象失去价值
- 访问私有变量对象
- C++访问具有相同名称的变量/对象的命名空间中的全局变量/对象
- C++-访问全局变量对象
- 表达式必须具有类类型:c++,将实例变量对象作为指针传递(点表示法参数)
- C++变量/对象和堆内存管理
- 如何在每次迭代中创建一个新变量/对象的循环
- 初始化现有变量/对象的新构造函数