新对象中的所有变量都设置为零,尽管构造函数中有默认参数。

huangapple go评论65阅读模式
英文:

All variables in new object are set to zero, despite default arguments in constructor

问题

I've translated the provided code for you:

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

class Weapon
public:
constructor()
[一些函数]
private:
[一些变量]
class WeaponShortBow:公开继承自Weapon
public:
constructor(默认参数)
[另一个函数]
private:
[无内容]

构造函数在其他类的对象的构造函数内调用。

以下是代码:

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 Ammunition;
    int Range;
    int TypeOfDice;
    int NumberOfDice;
    int HitModifier;
    bool IsFinesse;
    bool IsTwoHanded;
    bool IsMagical;
    bool IsLight;
    char Category;
    Effect *effect;
};

Weapon.cpp

#include "Weapon.h"

Weapon::Weapon(){
}

void Weapon::removeAmmo() {
    this->Ammunition += -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 Ammunition = 12,
                   int Range = 24,
                   int TypeOfDice = 6,
                   int NumberOfDice = 1,
                   int HitModifier = 0,
                   bool IsFinesse = 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 Ammunition,
        int Range,
        int TypeOfDice,
        int NumberOfDice,
        int HitModifier,
        bool IsFinesse,
        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;


<details>
<summary>英文:</summary>

I am writing a student project. For some reason all variables of this object are set to 0 or false, despite the fact that the constructor have default arguments set. The class of this object inherit after another class. So, it looks like this:

class Weapon
public:
constructor()
[some functions]
private:
[some variables]
class WeaponShortBow : inherit publicly after Weapon
public:
constructor (default arguments)
[one other function]
private:
[nothing]

The constructor is being called inside the constructor of object of other class.

here is code:


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;
}


the pointer to this object is stored here
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;

};

and here is 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(){}







I tried giving the values to the constructor in braces, but it does not seem to solve the problem. I also changed the type of pointer in actor.h from Weapon* to WeaponShortBow*, and instead of giving the default arguments in constructor, give them value in WeaponShortBow.h in private: like this
private:
int whatever = 2;

</details>


# 答案1
**得分**: 3

* `WeaponShortBow` 接受一系列参数,但并不对它们执行任何操作。它们不会自动存储在基类 `Weapon` 中。
* 目前,`WeaponShortBow` 没有办法将这些参数实际存储在基类中。成员变量是 `private` 的,而基类 `Weapon` 没有用于接受这些参数的构造函数。

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

```c++
class Weapon {
public:
    Weapon() = default; // 这种方式不需要提供空实现

    int getAmmo() const;

protected: // 如果只希望派生类能够使用构造函数
    Weapon(int Ammunition);

private:
    int m_ammunition = 0; // 在默认初始化时使用
};

class WeaponShortBow : public Weapon{
public:
    WeaponShortBow(int Ammunition = 12);
};

接受 Ammunition 参数的 Weapon 构造函数使用成员初始化列表来用构造函数接收的值初始化成员变量:

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

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

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

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

演示

英文:
  • WeaponShortBow takes a bunch of arguments, but does nothing with them. They are not automatically stored in the base class, Weapon.
  • Currently, there is no way for WeaponShortBow to actually store the arguments in the base class. The member variables are private and the base class, Weapon, has no constructor for accepting the arguments.

Consider this simplified version with only one of the arguments. Weapon here has a constructor taking the argument Ammunition. I've made it protected so that only derived classes can use it, but that's optional:

class Weapon {
public:
    Weapon() = default; // no need to provide an empty implementation this way

    int getAmmo() const;

protected: // if only derived classes should be able to use the constructor
    Weapon(int Ammunition);

private:
    int m_ammunition = 0; // used when default initialized
};

class WeaponShortBow : public Weapon{
public:
    WeaponShortBow(int Ammunition = 12);
};

The Weapon constructor taking the Ammunition parameter uses the member initializer list to initialize the member variable with the value given to the constructor:

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

Similarly, the WeaponShortBow constructor taking the Ammunition parameter uses that to initialize the base class:

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

So, the initialization goes:
WeaponShortBow constructor -> Weapon constructor -> Weapon member variable.

Demo

huangapple
  • 本文由 发表于 2023年6月22日 04:34:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定