Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6851871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:18:02+00:00 2026-05-27T01:18:02+00:00

My Class Hierarchy is thus: BaseClass (is an abstract class) then it has three

  • 0

My Class Hierarchy is thus: BaseClass (is an abstract class) then it has three subclasses that inherit from it: 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.

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?

I appreciate any insight

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:18:03+00:00Added an answer on May 27, 2026 at 1:18 am

    My vote goes to const values in the base class and a protected constructor:

    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:

    • No virtual dispatch at runtime (no overhead)
    • true const-ness (those values can’t be changed, ever)

    Virtual functions don’t give you const-ness, see this example:

    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;
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a class hierarchy that uses SSE intrinsics functions and thus some
I have the following class hierarchy public abstract BaseClass : System.Web.UI.UserControl { public virtual
I am using a hierarchy of generic collection classes that derive from an abstract
I have a simple Class Hierarchy that I am trying to get to work
I have following class hierarchy scenario; Class A has a method and Class B
Consider a class hierarchy where A is the base class and B derives from
I have a class hierarchy: abstract DomainObject { ... @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator=SEQ)
I have a python class hierarchy, that I want to extend at runtime. Furthermore
I have a C++ class hierarchy that looks something like this: class A; class
I have a class hierarchy that looks like this: class Base<TElement> { public TElement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.