Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Pointers to member functions are nothing like pointers to global functions or static member functions. There are many reasons for this, but I'm not sure how much you know about how C++ works, and so I'm not sure what reasons will make sense.</p> <p>I do know that what you are trying in assembly simply won't work in the general case. It seems like you have a fundamental misunderstanding about the purpose of member functions and function pointers.</p> <p>The thing is, you are doing some things that you would generally not do in C++. You don't generally build up tables of function pointers in C++ because the things you would use that sort of thing for are what <code>virtual</code> functions are for.</p> <p>If you are determined to use this approach, I would suggest you not use C++ at all, and only use C.</p> <p>To prove these pointer types are completely incompatible, here is a program for you:</p> <pre><code>#include &lt;cstdio&gt; struct Foo { int a; int b; int addThem() { return a + b; } }; struct Bar { int c; int d; int addThemAll() { return c + d; } }; struct Qux : public Foo, public Bar { int e; int addAllTheThings() { return Foo::addThem() + Bar::addThemAll() + e; } }; int addThemGlobal(Foo *foo) { return foo-&gt;a + foo-&gt;b; } int main() { int (Qux::*func)(); func = &amp;Bar::addThemAll; printf("sizeof(Foo::addThem) == %u\n", sizeof(&amp;Foo::addThem)); printf("sizeof(Bar::addThemAll) == %u\n", sizeof(&amp;Bar::addThemAll)); printf("sizeof(Qux::addAllTheThings) == %u\n", sizeof(&amp;Qux::addAllTheThings)); printf("sizeof(func) == %u\n", sizeof(func)); printf("sizeof(addThemGlobal) == %u\n", sizeof(&amp;addThemGlobal)); printf("sizeof(void *) == %u\n", sizeof(void *)); return 0; } </code></pre> <p>On my system this program yields these results:</p> <pre><code>$ /tmp/a.out sizeof(Foo::addThem) == 16 sizeof(Bar::addThemAll) == 16 sizeof(Qux::addAllTheThings) == 16 sizeof(func) == 16 sizeof(addThemGlobal) == 8 sizeof(void *) == 8 </code></pre> <p>Notice how the member function pointer is 16 bytes long. It won't fit into a <code>void *</code>. It isn't a pointer in the normal sense. Your code and <code>union</code> work purely by accident.</p> <p>The reason for this is that a member function pointer often needs extra data stored in it related to fixing up the object pointer it's passed in order to be correct for the function that's called. In my example, when called <code>Bar::addThemAll</code> on a <code>Qux</code> object (which is perfectly valid because of inheritance) the pointer to the <code>Qux</code> object needs to be adjusted to point at the <code>Bar</code> sub-object before the function is called. So <code>Qux::*</code>s to member functions must have this adjustment encoded in them. After all, saying <code>func = &amp;Qux::addAllTheThings</code> is perfectly valid, and if that function were called no pointer adjustment would be necessary. So the pointer adjustment is a part of the function pointer's value.</p> <p>And that's just an example. Compilers are permitted to implement member function pointers in any way they see fit (within certain constraints). Many compilers (like the GNU C++ compiler on a 64-bit platform like I was using) will implement them in a way that do not permit any member function pointer to be treated as at all equivalent to normal function pointers.</p> <p>There are ways to deal with this. The swiss-army knife of dealing with member function pointers is the <code>::std::function</code> template in C++11 or C++ TR1.</p> <p>An example:</p> <pre><code> #include &lt;functional&gt; // .... inside main ::std::function&lt;int(Qux *)&gt; funcob = func; </code></pre> <p><code>funcob</code> can point at absolutely anything that can be called like a function and needs a <code>Qux *</code>. Member functions, global functions, static member functions, functors... <code>funcob</code> can point at it.</p> <p>That example only works on a C++11 compiler though. But if your compiler is reasonably recent, but still not a C++11 compiler, this may work instead:</p> <pre><code> #include &lt;tr1/functional&gt; // .... inside main ::std::tr1::function&lt;int(Qux *)&gt; funcob = func; </code></pre> <p>If worse comes to worse, you can use the Boost libraries, which is where this whole concept came from.</p> <p>But I would rethink your design. I suspect that you will get a lot more milage out of having a well thought out inheritance hierarchy and using <code>virtual</code> functions than you will out of whatever it is you're doing now. With an interpreter I would have a top level abstract 'expression' class that is an abstract class for anything that can be evaluated. I would give it a virtual <code>evaluate</code> method. Then you can derive classes for different syntax elements like an addition expression a variable or a constant. Each of them will overload the evaluate method for their specific case. Then you can build up expression trees.</p> <p>Not knowing details though, that's just a vague suggestion about your design.</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