c++ new object中的所有变量都设置为零,尽管构造函数中有默认参数

zbsbpyhn  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(136)

我正在写一个学生项目。由于某种原因,这个对象的所有变量都被设置为0或false,尽管构造函数设置了默认参数。此对象的类继承于另一个类之后。所以,它看起来像这样:

  1. class Weapon
  2. public:
  3. constructor()
  4. [some functions]
  5. private:
  6. [some variables]
  7. class WeaponShortBow : inherit publicly after Weapon
  8. public:
  9. constructor (default arguments)
  10. [one other function]
  11. private:
  12. [nothing]

构造函数在其他类的对象的构造函数中被调用。
下面是代码:
Weapon.h

  1. #ifndef DNDTESTY_WEAPON_H
  2. #define DNDTESTY_WEAPON_H
  3. #include "Effects/Effect.h"
  4. class Weapon {
  5. public:
  6. Weapon();
  7. void removeAmmo();
  8. int getRange() const;
  9. int and_so_on() const;
  10. private:
  11. int Amunition;
  12. int Range;
  13. int TypeOfDice;
  14. int NumberOfDice;
  15. int HitModiff;
  16. bool IsFniesse;
  17. bool IsTwoHanded;
  18. bool IsMagical;
  19. bool IsLight;
  20. char Category;
  21. Effect *effect;
  22. };

Weapon.cpp

  1. #include "Weapon.h"
  2. Weapon::Weapon(){
  3. }
  4. void Weapon::removeAmmo() {
  5. this->Amunition += -1;
  6. }
  7. int Weapon::getRange() const {
  8. return Range;
  9. }
  10. int Weapon::and_so_on() const {
  11. return and_so_on;
  12. }

WeaponShortBow.h

  1. #include "Effects/Effect.h"
  2. #include "Effects/NoEffect.h"
  3. #include "Weapon.h"
  4. class WeaponShortBow : public Weapon{
  5. public:
  6. WeaponShortBow(int Amunition = 12,
  7. int Range = 24,
  8. int TypeOfDice = 6,
  9. int NumberOfDice = 1,
  10. int HitModiff = 0,
  11. bool IsFniesse = false,
  12. bool IsTwoHanded = true,
  13. bool IsMagical = false,
  14. bool IsLight = false,
  15. char Category = 'R');
  16. ~WeaponShortBow();
  17. Effect* ApplyShortBowEffect();
  18. private:
  19. };

WeaponShortBow.cpp

  1. #include "WeaponShortBow.h"
  2. #include "Effects/NoEffect.h"
  3. WeaponShortBow::WeaponShortBow(
  4. int Amunition,
  5. int Range,
  6. int TypeOfDice,
  7. int NumberOfDice,
  8. int HitModiff,
  9. bool IsFniesse,
  10. bool IsTwoHanded,
  11. bool IsMagical,
  12. bool IsLight,
  13. char Category){
  14. }
  15. Effect* WeaponShortBow::ApplyShortBowEffect(){
  16. Effect* tmp = new NoEffect;
  17. return tmp;
  18. }

指向该对象的指针存储在Actor. h中

  1. #include <string>
  2. #include "DiceRoller.h"
  3. #include "Weapons/Weapon.h"
  4. #include "Weapons/WeaponShortBow.h"
  5. #include "Weapons/Effects/Effect.h"
  6. using namespace std;
  7. class Actor {
  8. public:
  9. Actor(string name, char token);
  10. virtual ~Actor();
  11. string name;
  12. char token;
  13. int some_functions();
  14. private:
  15. int totalHP = 12;
  16. int lostHP = 0;
  17. int totalAC = 12;
  18. int lostAc = 0;
  19. int initiativeBonus = 0;
  20. int proficiency = 0;
  21. int speed = 6;
  22. int AbStr=14;
  23. int ModStr=(AbStr-10)/2;
  24. int AbDex=12;
  25. int ModDex=(AbDex-10)/2;
  26. int AbCon=12;
  27. int ModCon=(AbCon-10)/2;
  28. int AbInt=12;
  29. int ModInt=(AbInt-10)/2;
  30. int AbWis=12;
  31. int ModWis=(AbWis-10)/2;
  32. int AbCha=12;
  33. int ModCha=(AbCha-10)/2;
  34. Weapon* weapon;
  35. Effect* effect;
  36. };

这里是Actor.cpp

  1. #include "Actor.h"
  2. using namespace std;
  3. Actor::Actor(string name, char token) {
  4. this->name=name;
  5. this->token=token;
  6. this->weapon= new WeaponShortBow;
  7. }
  8. Actor::~Actor(){}
  9. void Actor::reciveDmg(int dmg){
  10. this->lostHP=(this->lostHP)+dmg;
  11. }
  12. void Actor::some other functions(){}

我尝试用大括号将值赋予构造函数,但似乎不能解决问题。我还将actor.h中指针的类型从Weapon* 更改为WeaponShortBow*,并不是在构造函数中给出默认参数,而是在WeaponShortBow.h中以private的方式给予它们赋值:像这样的private:int whatever = 2;

5cnsuln7

5cnsuln71#

  • WeaponShortBow接受一堆参数,但不对它们执行任何操作。它们不会自动存储在基类Weapon中。
  • 目前,WeaponShortBow没有办法在基类中实际存储参数。成员变量是private,基类Weapon没有接受参数的构造函数。

考虑这个只有一个参数的简化版本。这里的Weapon有一个接受参数Ammunition的构造函数。我将它设置为protected,这样只有派生类才能使用它,但这是可选的:

  1. class Weapon {
  2. public:
  3. Weapon() = default; // no need to provide an empty implementation this way
  4. int getAmmo() const;
  5. protected: // if only derived classes should be able to use the constructor
  6. Weapon(int Ammunition);
  7. private:
  8. int m_ammunition = 0; // used when default initialized
  9. };
  10. class WeaponShortBow : public Weapon{
  11. public:
  12. WeaponShortBow(int Ammunition = 12);
  13. };

接受Ammunition参数的Weapon构造函数使用member initializer list来初始化成员变量,该成员变量的值提供给构造函数:

  1. Weapon::Weapon(int Ammunition) : m_ammunition(Ammunition) {}
  2. int Weapon::getAmmo() const { return m_ammunition; }

类似地,接受Ammunition参数的WeaponShortBow构造函数使用它来初始化基类:

  1. WeaponShortBow::WeaponShortBow(int Ammunition) : Weapon(Ammunition) {}

因此,初始化是:
WeaponShortBow构造函数-> Weapon构造函数-> Weapon成员变量。
Demo

展开查看全部

相关问题