简体   繁体   English

C ++具有子类特定值的非变化成员变量

[英]C++ Non-changing member variables with sub-class specific values

My Class Hierarchy is thus: BaseClass (is an abstract class) then it has three subclasses that inherit from it: ArcaneWarrior, Guardian, Magi. 因此,我的类层次结构是:BaseClass(是一个抽象类)然后它有三个继承自它的子类:ArcaneWarrior,Guardian,Magi。

I want to have two non-changing values for DefaultHealth and DefaultMana that are specific to each subclass as they will all have different values for both variables. 我想为DefaultHealth和DefaultMana提供两个特定于每个子类的非变化值,因为它们对于两个变量都具有不同的值。

I guess I'm just looking for the best/most efficient way to do this. 我想我只是在寻找最好/最有效的方法来做到这一点。

Should I just have two virtual functions in the base class to return DefaultHealth and DefaultMana, and in the subclasses hard code in the values I want? 我应该只在基类中有两个虚函数来返回DefaultHealth和DefaultMana,而在子类中我想要的值是硬代码吗?

I appreciate any insight 我很欣赏任何见解

My vote goes to const values in the base class and a protected constructor: 我的投票转到基类中的const值和protected构造函数:

class BaseClass{
  const unsigned default_health;
  const unsigned default_mana;

protected:
  BaseClass(unsigned def_hp, unsigned def_mp)
    : default_health(def_hp)
    , default_mana(def_mp)
  {
  }

public:
  // your functions...
};

class ArcaneWarrior
  : public BaseClass
{
public:
  ArcaneWarrior()
    : BaseClass(200, 50)
  {
  }

  // ...
};

This is superior to the virtual function approach in 2 ways: 这在两个方面优于virtual函数方法:

  • No virtual dispatch at runtime (no overhead) 运行时没有虚拟调度(没有开销)
  • true const -ness (those values can't be changed, ever) true const -ness(这些值无法更改,永远)

Virtual functions don't give you const -ness, see this example: 虚函数不提供const -ness,请参阅此示例:

class BaseClass{
public:
  virtual unsigned GetDefaultHealth() const = 0;
  virtual unsigned GetDefaultMana() const = 0;
  // ...
};

class ArcaneWarrior
  : public BaseClass
{
  unsigned default_health, default_mana;

public:
  virtual unsigned GetDefaultHealth() const{
    return default_health;
  }

  virtual unsigned GetDefaultHealth() const{
    return default_mana;
  }

  void SetDefaults(unsigned health, unsigned mana){
    default_health = health;
    default_mana = mana;
  }
};

Yes, that's really the best and most straightforward way to do it. 是的,这确实是最好,最直接的方式。 With virtual functions, you get both the "non-changing"-ness you want, along with being able to determine that value through a pointer (or reference) to the base class without knowing the exact type. 使用虚函数,您既可以获得所需的“不变”,又可以通过指向基类的指针(或引用)来确定该值,而无需知道确切的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转换指向子类的指针(C ++) - Casting a pointer to a sub-class (C++) Class 子类中的模板非类型成员访问 - Class Template non-type member access in sub-class 子类无法访问C ++公共非虚拟基类方法 - C++ public non-virtual base class method is inaccessible from sub-class 析构函数中C ++ Glib :: Object子类的g_object_unref()成员指针? - g_object_unref() member pointer of C++ Glib::Object sub-class in destructor? C++ 中模板化子类专业化的限制 - Limits of templated sub-class specialization in C++ c++ - 如何通过使用c ++中的构造函数获取父类的变量来在子类中实现抽象类的方法 - How to implement an abstarct class's method in sub-class by getting variables of it's parents class using a constructor in c++ 避免 C++ 中重复的子类定义 - Avoiding repetitive sub-class definitions in C++ C ++继承:将子类传递给期望基类的函数并获得子类行为 - C++ Inheritance: Pass sub-class to a function expecting base-class and get sub-class behaviour C ++非虚拟类成员变量的内存布局? - C++ non-virtual class member variables memory layout? C ++-类成员函数中的数组值自发更改 - C++ - Array values in class member function changing spontaneously
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM