Can you inherit two classes that share the same pure virtual member? I want to make a base object class that can be turned into different shapes. example:
I think you'll find this is what's called multiple inheritance and it is usually a bad thing, but as to if it is possible, try it and see if it works. I would suggest inheriting Wall and Player from Entity rather than the shapes though, because then they are all drawable and it isn't inherited multiple times.
this shows what should be in target.draw() (a drawable object), but the object IS the drawable object. so i'm stumped..
>I would suggest inheriting Wall and Player from Entity rather than the shapes though, because then they are all drawable and it isn't inherited multiple times.
how would that be possible? the point is for the objects to be different shapes. if I don't inherit any shape then it's just a drawable class. I'd still have to inherit sf::Transformable as well.
// Example program
#include <iostream>
#include <string>
class A {
public:
virtualvoid print() const = 0;
};
class B : public A {
public:
virtualvoid print() const { std::cout << "B" << std::endl; }
};
class C : public A {
public:
virtualvoid print() const { std::cout << "C" << std::endl; }
};
class D : public B, public C {
public:
virtualvoid print() const { std::cout << "D" << std::endl; }
};
int main()
{
D d;
d.print();
}
The problem is that this is not a good idea.
You are using inheritance for code reuse but in this case you should prefer composition over inheritance.
imagine you have a car, would you inherit from sf::CircleShape 4 times because you have 4 wheels?
I think not, well, because you can't inherit 4 times from the same class.
Your objects are not shapes, they have a shapes.
This being said, i think your objects are still Drawable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class Entity : public sf::Drawable
{
// ...
};
class Wall : public sf::Drawable
{
// ...
private:
sf::RectangleShape shape_;
};
class Player : public sf::Drawable
{
// ...
private:
sf::CircleShape shape_;
};