Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Function Pointers using class T
    primarykey
    data
    text
    <p>I am working on my own GUI framework using C++ and OpenGL. My experience in both is small as this is my first project using them.</p> <p>In the framework I have created a window, which renders the design of the window, and then any objects that have been added to the window as a child.</p> <p>I then create the window using a new class called MainMenu that inherits from my window class, I then add a new button to the MainMenu</p> <p>Using:</p> <pre><code>button-&gt;SetOnClick(&amp;MainMenu::button_Click, this); </code></pre> <p>I then add a pointer to the function, as well as the instance of the class to the buttons 'SetOnClick' method - Therefore if that button is clicked a method within the MainMenu will run.</p> <p>This works great at the moment, however my issue now is that I want to add a new window called Settings, which will also inherit from the Window class</p> <p>Therefore I need to make the function pointer in my button class work for any number of classes (all of type Window)</p> <p>Here is the code that is currently working - but for only the MainMenu (Please note that I have stripped out a lot of methods that do not relate to the question to make it easier to read)</p> <p>Window.h:</p> <pre><code>class Window { private: void Initialize(WindowManager* manager, int width, int height); public: Window(WindowManager* manager); Window(WindowManager* manager, int width, int height); ~Window(void); void Render(); bool MouseMove(int x, int y); bool MouseLBDown(int x, int y); Children* Child() { return m_children; }; </code></pre> <p>MainMenu.h:</p> <pre><code>class MainMenu : public ObjWindow { private: void Initialize(); public: MainMenu(WindowManager* manager, int width, int height) : Window(manager, width, height) { Initialize(); }; MainMenu(WindowManager* manager) : Window(manager) { Initialize(); }; ~MainMenu(void); void button_Click(); </code></pre> <p>MainMenu.cpp:</p> <pre><code>void MainMenu::Initialize() { Button* button = new Button(Vector2D(100, -400), 100, 50); Child()-&gt;Add(button); button-&gt;SetText("Click Me!"); button-&gt;SetOnClick(&amp;MainMenu::button_Click, this); } void MainMenu::button_Click() { //Do some button code } </code></pre> <p>Button.h:</p> <pre><code>class Button { typedef void (MainMenu::*Function)(); Function m_onClickFunction; MainMenu* m_window; public: Button(Vector2D position, int width, int height); ~Button(void); void SetText(std::string); std::string GetText(); void Render(); bool MouseMove(int x, int y); bool MouseLBDown(int x, int y); void SetOnClick(Function, MainMenu*); </code></pre> <p>Button.cpp:</p> <pre><code>Button::Button(Vector2D position, int width, int height) { m_onClickFunction = NULL; } Button::~Button(void) { } bool Button::MouseMove(int x, int y) { return true; } bool Button::MouseLBDown(int x, int y) { if (m_body-&gt;Intercepts(x,y)) { if (m_onClickFunction != NULL) { (m_window-&gt;*m_onClickFunction)(); } } return true; } void Button::SetOnClick(Function function, MainMenu* window) { m_onClickFunction = function; m_window = window; } </code></pre> <p>From researching around the internet it seems that I need to use a template so that I can handle the class as a type, I have tried fitting this in to my code, but so far I nothing has worked.</p> <p>Please note that I am also new to asking question on stackoverflow, so if I am missing anything important please let me know</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload