This is baffling me. Below we have three classes, the Paddle class and two wrapper classes that call members of the Paddle object.
The Paddle object isn't working with the delegates, but works when invoked from wrapper classes. All functions have the same signatures and overloads.
The compiler throws no errors, and the code runs fine but has the above unexpected behavior.
Why is this happening?
Display All
The Paddle object isn't working with the delegates, but works when invoked from wrapper classes. All functions have the same signatures and overloads.
The compiler throws no errors, and the code runs fine but has the above unexpected behavior.
Why is this happening?
Source Code
- // sf::Transformable has 2 overloaded member functions:
- // void setPosition(float x, float y);
- // void setPosition(const Vector2f& position);
- class Paddle : public sf::Drawable, public sf::Transformable {}
- Paddle paddle{};
- // wrap the calls in another class with the same overload signatures as paddle
- // used to show delegate working with overloads
- class A
- {
- public:
- void setPosition(sf::Vector2f& v) {
- paddle.setPosition(v);
- }
- void setPosition(float x, float y) {
- paddle.setPosition(x, y);
- }
- };
- // inherit members from A
- // used to show delegate working with inherited overloads
- class B : public A {};
- // delegate typedef
- typedef std::function<void(float,float)> TranslateDelegate;
- // create wrapper classes
- A a{};
- B b{};
- // create 3 delegates
- TranslateDelegate pda = std::bind(static_cast<void(A::*)(float, float)>(&A::setPosition), a, std::placeholders::_1, std::placeholders::_2);
- TranslateDelegate pdb = std::bind(static_cast<void(B::*)(float, float)>(&B::setPosition), b, std::placeholders::_1, std::placeholders::_2);
- TranslateDelegate pdp = std::bind(static_cast<void(Paddle::*)(float, float)>(&Paddle::setPosition), paddle, std::placeholders::_1, std::placeholders::_2);
- pda(300.f, 900.f); // sets paddle position correctly
- pdb(300.f, 900.f); // sets paddle position correctly
- pdp(300.f, 900.f); // DOES NOTHING!!