Note that there are some explanatory texts on larger screens.

plurals
  1. POFunction pointer to a non-static member function when the class type is unknown?
    primarykey
    data
    text
    <p>I'm working on a game project that features scratch-built controls rendered into an opengl context; things like buttons, scrollbars, listboxes, etc. Many of these controls are nested; for example, my listbox has a scrollbar, a scrollbar has 3 buttons, etc. </p> <p>When a scrollbar changes value, I'd like it to call 'some' function (typically in it's parent object) that responds to the change. For example, if the listbox has a slider, it should instantiate the slider, then tell the new slider that it should call the listboxes 'onScroll(float)' function. All of the controls share a common base class, so I could have a 'base* parent' parent pointer, then do 'parent->onScroll(val)'. The problem though is what happens when the parent doesn't inheirit from base; there'd be no virtual onScroll() to follow through, so the top-level parent would have to periodically check to see if any of the child controls had changed value. This would also clutter up other controls, since they may not even have children, or may require different event types like when a list entry object is selected, etc.</p> <p>A better solution would be to have the child object maintain a generic function pointer (like a callback), which can be set by the parent, and called by the child as necessary. Something like this:</p> <pre><code>typedef (*ptFuncF)(float); class glBase { public: //position,isVisible,virtual mouseDown(x,y),etc }; class glDerivedChild : public glBase { public: glDerivedChild(); ~glDerivedChild(); void changeValue(float fIn) { Value = fIn; //ignore these forward declaration errors (*callBack)(fIn); } void setCallBack(ptFuncF pIn) {callBack = pIn;} ptFuncF callBack; float Value; }; class glDerivedParent : public glBase { public: glDerivedParent() { child = new glDerivedChild(); child-&gt;setCallBack(&amp;onScroll); } ~glDerivedParent() {delete child;} void onScroll(float fIn) { //do something } glDerivedChild* child; }; class someFoo { public: someFoo() { child-&gt;setCallBack(&amp;setValue); } void setValue(float fIn) { //do something else } glDerivedChild child; }; </code></pre> <p>I'm kinda new to function pointers, so I know I'm (obviously) doing many things wrong. I suspect it might involve something like "typedef (glBase::*ptFuncF)(float);" with the 'onScroll(f)' being an overridden virtual function, perhaps with a generic name like 'virtual void childCallBack(float)'. I'd prefer to keep the solution as close to vanilla as possible, so I want to avoid external libraries like boost. I've been scratching my head over this one for the better part of 8 hours, and I'm hoping someone can help. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    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