|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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
Code:
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 &);
};
Code:
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);
}
Code:
O3DMVector.cpp:5: error: cannot convert ‘float (*)(O3DMVector&)’ to ‘float (O3DMVector::*)(O3DMVector&)’ in initialization Code:
float O3DMVector::(*getLengthPtr)(O3DMVector &) = &cGetLength; Code:
O3DMVector.cpp:5: error: expected unqualified-id before ‘(’ token
__________________
Currently bemused by: LLVM. |
| Sponsored Links |
|
|
|
|||
|
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;
__________________
Currently bemused by: LLVM. |
|
|||
|
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 09:36 PM. Reason: Fixed typo |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| function pointers | porno.shome | Pascal/Delphi | 5 | 07-26-2008 02:42 AM |
| C#:Tutorial - Download Data | Xav | CSharp Tutorials | 9 | 04-23-2008 03:18 PM |
| using Template function | Chinmoy | C Tutorials | 4 | 04-03-2008 04:16 AM |
| Derived Classes Can't Set Function Pointers | dargueta | C and C++ | 11 | 11-05-2007 09:04 PM |
| John | ........ | 223.00000 |
| dargueta | ........ | 168.00000 |
| Xav | ........ | 164.00000 |
| gaylo565 | ........ | 18.00000 |
| WingedPanther | ........ | 15.00000 |
| |pH| | ........ | 15.00000 |
| Johnnyboy | ........ | 3.00000 |
| navghost | ........ | 1.00000 |
Goal: 100,000 Posts
Complete: 65%