我正在写一个学生项目。由于某种原因,这个对象的所有变量都被设置为0或false,尽管构造函数设置了默认参数。此对象的类继承于另一个类之后。所以,它看起来像这样:
class Weapon
public:
constructor()
[some functions]
private:
[some variables]
class WeaponShortBow : inherit publicly after Weapon
public:
constructor (default arguments)
[one other function]
private:
[nothing]
构造函数在其他类的对象的构造函数中被调用。
下面是代码:
Weapon.h
#ifndef DNDTESTY_WEAPON_H
#define DNDTESTY_WEAPON_H
#include "Effects/Effect.h"
class Weapon {
public:
Weapon();
void removeAmmo();
int getRange() const;
int and_so_on() const;
private:
int Amunition;
int Range;
int TypeOfDice;
int NumberOfDice;
int HitModiff;
bool IsFniesse;
bool IsTwoHanded;
bool IsMagical;
bool IsLight;
char Category;
Effect *effect;
};
Weapon.cpp
#include "Weapon.h"
Weapon::Weapon(){
}
void Weapon::removeAmmo() {
this->Amunition += -1;
}
int Weapon::getRange() const {
return Range;
}
int Weapon::and_so_on() const {
return and_so_on;
}
WeaponShortBow.h
#include "Effects/Effect.h"
#include "Effects/NoEffect.h"
#include "Weapon.h"
class WeaponShortBow : public Weapon{
public:
WeaponShortBow(int Amunition = 12,
int Range = 24,
int TypeOfDice = 6,
int NumberOfDice = 1,
int HitModiff = 0,
bool IsFniesse = false,
bool IsTwoHanded = true,
bool IsMagical = false,
bool IsLight = false,
char Category = 'R');
~WeaponShortBow();
Effect* ApplyShortBowEffect();
private:
};
WeaponShortBow.cpp
#include "WeaponShortBow.h"
#include "Effects/NoEffect.h"
WeaponShortBow::WeaponShortBow(
int Amunition,
int Range,
int TypeOfDice,
int NumberOfDice,
int HitModiff,
bool IsFniesse,
bool IsTwoHanded,
bool IsMagical,
bool IsLight,
char Category){
}
Effect* WeaponShortBow::ApplyShortBowEffect(){
Effect* tmp = new NoEffect;
return tmp;
}
指向该对象的指针存储在Actor. h中
#include <string>
#include "DiceRoller.h"
#include "Weapons/Weapon.h"
#include "Weapons/WeaponShortBow.h"
#include "Weapons/Effects/Effect.h"
using namespace std;
class Actor {
public:
Actor(string name, char token);
virtual ~Actor();
string name;
char token;
int some_functions();
private:
int totalHP = 12;
int lostHP = 0;
int totalAC = 12;
int lostAc = 0;
int initiativeBonus = 0;
int proficiency = 0;
int speed = 6;
int AbStr=14;
int ModStr=(AbStr-10)/2;
int AbDex=12;
int ModDex=(AbDex-10)/2;
int AbCon=12;
int ModCon=(AbCon-10)/2;
int AbInt=12;
int ModInt=(AbInt-10)/2;
int AbWis=12;
int ModWis=(AbWis-10)/2;
int AbCha=12;
int ModCha=(AbCha-10)/2;
Weapon* weapon;
Effect* effect;
};
这里是Actor.cpp
#include "Actor.h"
using namespace std;
Actor::Actor(string name, char token) {
this->name=name;
this->token=token;
this->weapon= new WeaponShortBow;
}
Actor::~Actor(){}
void Actor::reciveDmg(int dmg){
this->lostHP=(this->lostHP)+dmg;
}
void Actor::some other functions(){}
我尝试用大括号将值赋予构造函数,但似乎不能解决问题。我还将actor.h中指针的类型从Weapon* 更改为WeaponShortBow*,并不是在构造函数中给出默认参数,而是在WeaponShortBow.h中以private的方式给予它们赋值:像这样的private:int whatever = 2;
1条答案
按热度按时间5cnsuln71#
WeaponShortBow
接受一堆参数,但不对它们执行任何操作。它们不会自动存储在基类Weapon
中。WeaponShortBow
没有办法在基类中实际存储参数。成员变量是private
,基类Weapon
没有接受参数的构造函数。考虑这个只有一个参数的简化版本。这里的
Weapon
有一个接受参数Ammunition
的构造函数。我将它设置为protected
,这样只有派生类才能使用它,但这是可选的:接受
Ammunition
参数的Weapon
构造函数使用member initializer list来初始化成员变量,该成员变量的值提供给构造函数:类似地,接受
Ammunition
参数的WeaponShortBow
构造函数使用它来初始化基类:因此,初始化是:
WeaponShortBow
构造函数->Weapon
构造函数->Weapon
成员变量。Demo