Multiple Inheritance - Method Naming Conflicts

This is my class header:
1
2
3
4
5
6
7
8
9
10
11
#include <SFML/Graphics.hpp>

class RectSolid : public sf::Shape, sf::Rect<float> {
public:
    RectSolid();
    RectSolid(float x, float y, float width, float height);
    ~RectSolid();
    
    void RectSolid::Move(float XOffSet, float YOffSet);
    void RectSolid::SetPosition(float XOffSet, float YOffSet);
};

and .cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "rectsolid.h"

RectSolid::RectSolid()
{
}

RectSolid::RectSolid(float X, float Y, float Width, float Height)
{
    Left = X;
    Top = Y;
    Right = X + Width;
    Bottom = Y + Height;
    
    AddPoint(sf::Vector2f(Left, Top));
    AddPoint(sf::Vector2f(Right, Top));
    AddPoint(sf::Vector2f(Right, Bottom));
    AddPoint(sf::Vector2f(Left, Bottom));
}

RectSolid::~RectSolid()
{
}


Notice the Move and SetPosition methods in the .h file. They are not in the .cpp file because there is a Move and SetPosition in the sf::Shape class. How can I define these methods without conflicting with the sf::Shape's corresponding methods? I actually want to call the sf::Shape::Move method in the RectSolid::Move method. Same thing with SetPosition. How can I make it explicit which one I'm referring to?

Thanks,
JCED
How can I define these methods without conflicting with the sf::Shape's corresponding methods?


Don't define them in RectSolid at all. RectSolid will inhert all of its parent's members automatically.

Side note: you should probably make inheritance public for all parent classes (I assume this was your intent):

 
class RectSolid : public sf::Shape, public sf::Rect<float> // each parent needs 'public' 
Don't define them in RectSolid at all. RectSolid will inhert all of its parent's members automatically.


Yes, but I want to override the other class's definition, but use it in my own definition at the same time. My alternative would be to do this:
1
2
3
4
5
void Slide(float XOffSet, float YOffSet)
{
    Move(XOffSet, YOffSet);
    OffSet(XOffSet, YOffSet);
}

The reason for this is because the sf::Rect class has a method called 'OffSet' which is equivalent to sf::Shape's move, just on the Rect, not the Shape.

To be clear, the difference between sf::Shape and sf::Rect is that sf::Rect is just coordinates that make it easier to manipulate sf::Shapes and sf::Sprites, while sf::Shape is a drawable object which can be moved, but is a pain to manipulate if not paired with an sf::Rect. I'm trying to make a class that brings them together and so that I can manipulate it without having to worry about sf::Rect's coordinates matching sf::Shape's.

To be even clearer, the Left, Top, Right, Bottom in the constructor are members of sf::Rect, while the AddPoint method is part of the sf::Shape.
Side note: you should probably make inheritance public for all parent classes (I assume this was your intent):

class RectSolid : public sf::Shape, public sf::Rect<float> // each parent needs 'public'

Yes, that was my intent :). Thanks.
Last edited on
I ended up renaming the Move to Slide and SetPosition to MoveTo to avoid naming conflicts, although I'm still interested in how I could do it using the conflicting names.
Ooooohhh... I'm sorry, I totally misunderstood. When you said "I actually want to call the sf::Shape::Move method in the RectSolid::Move method", I thought you meant the functions would be the same. My mistake.

Can you change sf::Shape or is it part of some library you're using?



If you can change it, I would suggest making the functions in question virtual. Then you can simply reimplement them and call the parent's function from the derived function.

1
2
3
4
5
6
7
8
class RectSolid : ...
{
  virtual void Move(float XOffSet, float YOffSet)  // make it virtual in sf::Shape too!
  {
    sf::Shape::Move(XOffSet,YOffSet);
    Offset(XOffset,YOffset);
  }
};


If you can't change sf::Shape to make that function virtual, you can still do it this way, although it's riskier, because sf::Shape::Move might be called by mistake instead of the intended RectSolid::Move. This would happen if you have a sf::Shape pointer which points to a RectSolid.

Although it might be a better way to go to use a function with a different name. It depends on how you want this class to behave.
Ooooohhh... I'm sorry, I totally misunderstood. When you said "I actually want to call the sf::Shape::Move method in the RectSolid::Move method", I thought you meant the functions would be the same. My mistake.


I figured. That's why I tried to clarify what was going on in my reply.

Can you change sf::Shape or is it part of some library you're using?


Library. SFML, specifically.

If you can't change sf::Shape to make that function virtual, you can still do it this way, although it's riskier, because sf::Shape::Move might be called by mistake instead of the intended RectSolid::Move. This would happen if you have a sf::Shape pointer which points to a RectSolid.


Thanks. I don't plan on making sf::Shape pointers. In fact, with my RectSolid, I plan not to use sf::Shapes in the main program's body at all. I'll be creating other classes that combine sf::Rects with sf::Somethings for the convenience, and hope to never have to touch much sf:: stuff ever again.

Again, thanks. This solves all my problems.
Topic archived. No new replies allowed.