0

I want to resolve an inheritance problem of my Paladin class. Here is a resume of the inheritance between my classes :

class Peasant
{
    virtual int attack();
}

class Knight : virtual public Peasant
{
    int attack() override;
}

class Enchanter : public Peasant
{
    int attack() override;
}

class Priest : virtual public Enchanter

class Paladin : public Knight, public Priest
{
    using Knight::attack();
}

This way I got the following error :

Paladin.hpp:16:7: error: no unique final redefinition « virtual int Peasant::attack() » in « Paladin »
   16 | class Paladin : public Knight, public Priest
      |       ^~~~~~~

Words may differ in the official error message as I traduce this error from another language to english.

I solved my problem this way :

class Peasant
{
    int attack();
}

class Knight : virtual public Peasant
{
    int attack();
}

class Enchanter : public Peasant
{
    int attack();
}

class Priest : virtual public Enchanter

class Paladin : public Knight, public Priest
{
    using Knight::attack();
}

But I don't really like this solution. I prefer using virtual and override when I override the base's methods in derived classes. I know I could do :

class Peasant
{
    virtual int attack();
}

class Knight : virtual public Peasant
{
    virtual int attack() override;
}

class Enchanter : public Peasant
{
    virtual int attack() override;
}

class Priest : virtual public Enchanter

class Paladin : public Knight, public Priest
{
    int attack() override {return Knight::attack();};
}

But in the case I'm just re-using a method, I think it's more elegant to use using.

So I would like to know if there is a way to use both virtual/override and using in the same syntax.

Thank you in advance :D

8
  • 2
    What would be the purpose of adding an override to a using? The using does not override anything Commented Jan 22, 2024 at 14:34
  • And it makes no sense to inherit the same class both virtually and on-virtually. The result is utter confusion. Perhaps rethink the relationship between the various classes in this hierarchy? Commented Jan 22, 2024 at 14:36
  • Also none of the examples actually compile due to various syntax and access specifier errors, making it very hard to tell what your original problem was Commented Jan 22, 2024 at 14:36
  • 3
    Rule of thumb: virtual inheritance everywhere or nowhere. in Enchanter, Peasant should be a virtual base. Commented Jan 22, 2024 at 14:47
  • 1
    "I solved my problem this way :...." its not just your personal preference, but overriding a method does require the method to be virtual. You cannot override a non virtual method and in that middle code nothing is being overridden Commented Jan 22, 2024 at 15:40

1 Answer 1

2

Your second option is the correct way to go. using doesn't define a function, it brings a name into scope.

You also have another error, attack is private in each of these classes, so you can't return Knight::attack().

Not a syntax error, but you are missing virtual off your inheritance at least in Enchanter, and probably in Paladin.

struct Peasant
{
    virtual int attack();
};

struct Knight : virtual Peasant
{
    int attack() override;
};

struct Enchanter : virtual Peasant
{
    int attack() override;
};

struct Priest : virtual Enchanter
{};

struct Paladin : virtual Knight, virtual Priest
{
    int attack() override { return Knight::attack(); };
};

However, trying to embed the rules of a game into subclasses within an inheritance hierarchy can be like fitting a round peg in a square hole.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.