Simply enough I'm writing a math library that detects the abilities of the CPU (whether it has 3DNow, SSE, etc) and then modifies the behaviour of some class members to utilise an optimal implementation. I'm having trouble setting up the pointers correctly. I have the class definition
I also have some implementation detailsCode:class O3DMVector { public: float x, y, z, w; O3DMVector() { x = 0.0f; y = 0.0f; z = 0.0f; w = 1.0f; } inline float getLength() { return (*getLengthPtr)(*this); } private: static float (*getLengthPtr)(O3DMVector &); };
This gives me the error outputCode:float cGetLength(O3DMVector &v); float (O3DMVector::*getLengthPtr)(O3DMVector &) = &cGetLength; float cGetLength(O3DMVector &v) { return sqrt(v.x * v.x + v.y * v.y + v.z * v.z); }
I've tried modifying line 5 to readCode:O3DMVector.cpp:5: error: cannot convert ‘float (*)(O3DMVector&)’ to ‘float (O3DMVector::*)(O3DMVector&)’ in initialization
This gives a different errorCode:float O3DMVector::(*getLengthPtr)(O3DMVector &) = &cGetLength;
Anyone know what I have to do to get this working?Code:O3DMVector.cpp:5: error: expected unqualified-id before ‘(’ token
First of all, it looks to me like you're returning a function pointer to a private function, which is a no-no. Is the declaration of cGetLength inside the O3DMVector declaration? If it's not, then you have another problem. Even though the functions have the same arguments and return types, their pointers are incompatible because C/C++ passes a hidden this pointer in all class functions.
I'm not returning a pointer to a private function. I have a function pointer as a private static data member.
The cGetLength function is the implementation (in plain C/C++). The argument v to cGetLength is being used in place of the this pointer (note that the implementation has one more variable than the inlined method that calls the function pointer). Eventually there will be an sseGetLength and possibly an amd3DNowGetLength. Also an initGetLength which calls my CPU testing code on the first run and picks the appropriate implementation for future calls.
I actually solved this about 45 minutes ago by altering the declaration*. Apparently there's a mess in C++ what's a member function pointer and what's a normal function pointer. What I've defined there is the former when it needs to be the latter. It passes the test suite now so either god is fiddling the values or it works.
* float (*O3DMVector::getLengthPtr)(O3DMVector &) = &cGetLength;
Yep, you've got it. Like I said, function pointers are different for classes than for "regular" function pointers - they don't have the same signature or calling convention. Member functions use __thiscall and "regular" functions typically use __stdcall.
Last edited by dargueta; 07-07-2008 at 07:36 PM. Reason: Fixed typo
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks