Base class question!

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:

sf::Drawable - http://www.sfml-dev.org/documentation/2.2/Drawable_8hpp_source.php

sf::RectangleShape - http://www.sfml-dev.org/documentation/2.2/RectangleShape_8hpp_source.php

sf::CircleShape - http://www.sfml-dev.org/documentation/2.2/CircleShape_8hpp_source.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Entity : public sf::Drawable
{

};

class Wall : public sf::RectangleShape
{

};

class Player : public sf::CircleShape
{

};



The reason why I need Entity to be able to be drawn is because I'm storing/drawing my objects like this:

1
2
3
4
5
6
7
8
9
//init
std::vector<Entity*> entities;

//draw
for(auto it = entities.begin(); it != entities.end(); it++)
{
     auto entity = **it;
     window.draw(entity);
}


I also need the base class to be sf::Transformable.. But I feel like if I understand the sf::Drawable part I'll know how to do that as well.
Last edited on
Why not just give each class a sprite and change the texture?
I mean I'd like to know if this is possible regardless... Seems like an important thing to know if you can or can't. Also just because I want to!
Last edited on
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.
I mean I can do it. I just don't know what to put in the draw function.

http://www.sfml-dev.org/documentation/2.0/classsf_1_1Drawable.php

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.
Last edited on
May I suggest taking this up on the SFML forums? There are more people there who will help with this as it is becoming very specific to SFML.
Lot's of people use SFML on here, and I prefer this forum over SFML's. But if I don't really get any info I'll try over there. Thanks!
yeah, it looks like it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Example program
#include <iostream>
#include <string>

class A {
public:
    virtual void print() const = 0;
};

class B : public A {
public:
    virtual void print() const { std::cout << "B" << std::endl; }
};
class C : public A {
public:
    virtual void print() const { std::cout << "C" << std::endl; }
};

class D : public B, public C {
public:
    virtual void print() const { std::cout << "D" << std::endl; }
};

int main()
{
    D d;
    d.print();
}
http://cpp.sh/6ydb

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_;
};
Topic archived. No new replies allowed.