Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The error is most likely from the use of the function pointer to point to member functions. There is a considerable difference between functions that are in classes and just normal functions. You are assigning a class function to a normal function pointer. The difference comes in when you consider that all member functions take a hidden <code>this</code> pointer as their first argument.</p> <p>Either change the functions to be outside of the class (or instead use static functions if the functions do not need any of the class's variables, bases or the <code>this</code> pointer) or change the function pointer to a class member function pointer. A pointer to a MyClass function that takes a string will look like this.</p> <pre><code>void (MyClass::*fptr)(std::string str); </code></pre> <p>Because the class function pointer requires a hidden <code>this</code> pointer the call of the function pointer changes also. To call the function pointed to by fptr you can use the <code>-&gt;*</code> or <code>.*</code> c++ operators. So to call it using the <code>this</code> pointer of MyClass you can do this:</p> <pre><code>std::string aString; (this-&gt;*fptr)(aString); </code></pre> <p>I'm not entirely sure but it maybe possible to do what you hope to do with virtual functions instead? This can be achieved by having two separate classes and a pointer to an instance of one of the two. Both classes derive from a class that has a pure virtual function that is the same as the function you are assigning here. This is a cleaner solution than using function pointers. I would look into this as you may find it useful if your looking for dynamic behavior using function pointers.</p> <p>This is a good tutorial on the basics of function pointers:</p> <p><a href="http://www.learncpp.com/cpp-tutorial/78-function-pointers/" rel="nofollow">http://www.learncpp.com/cpp-tutorial/78-function-pointers/</a></p>
 

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