0

I've an issue when i try to call a constructor that require a special implementation of an interface.

To make it cleaner now, here's the code :

I_Window* window = new GLFW_Window(800,600,"Learn OpenGL");
I_Input_Manager* manager = new GLFW_Input_Manager(*window);

And the class implementing the interface is like so :

class GLFW_Input_Manager : public I_Input_Manager
{
    private:

    GLFW_Window& window_ref;

    public:

    GLFW_Input_Manager(GLFW_Window& window_ref_);
    virtual ~GLFW_Input_Manager();
    //...
};

The error return by the compiler is :

error: no matching function for call to ‘GLFW_Input_Manager::GLFW_Input_Manager(I_Window&)’
 I_Input_Manager* manager = new GLFW_Input_Manager(*window);

I understand the problem, my constructor need as argument an "I_Window" but for this special implementation, i need a reference to a GLFW_Window because the real implementation gonna use the GLFWwindow.

So how can i solve this problem ?

Thanks !

LenweSeregon
  • 132
  • 1
  • 10
  • 3
    Did you try using `GLFW_Window*` instead of `I_Window*` as a type specifier of `window`? Or `static_cast` if that is not possible? – Algirdas Preidžius Jan 20 '18 at 17:38
  • Indeed, it's gonna work but i don't want to loose the power of polymorphism by giving the real type of the window (maybe it's nosense ? i don't know tbh ...) and casting an abstract class is not possible ? It is the only solution to give the real type of window ? – LenweSeregon Jan 20 '18 at 17:42
  • 1
    Add a virtual `create_input_manager()` method to the `I_Window` base class, and implement it appropriately in the subclass. – Sam Varshavchik Jan 20 '18 at 17:43
  • Ohhh yeah i had not thought about it ! Thanks a lot ! Have a nice day ! – LenweSeregon Jan 20 '18 at 17:46
  • @LenweSeregon 1) How will you lose polymorphism? Derived classes already have everything that base classes do. You are using a pointer to an object in either case. Can you explain, **why** do you think, that anything will be lost? 2) How is casting not possible, in the described situation? Casting to a pointer to abstract type shouldn't be a problem, did you try it? – Algirdas Preidžius Jan 20 '18 at 18:10

0 Answers0