View Single Post
  #1 (permalink)  
Old 07-05-2008, 11:35 AM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 473
Last Blog:
Just over the next hil...
Rep Power: 10
G_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura about
Default C++ - function pointers as a static data member.

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 &);
};
I also have some implementation details

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);
}
This gives me the error output

Code:
O3DMVector.cpp:5: error: cannot convert ‘float (*)(O3DMVector&)’ to ‘float (O3DMVector::*)(O3DMVector&)’ in initialization
I've tried modifying line 5 to read

Code:
float O3DMVector::(*getLengthPtr)(O3DMVector &) = &cGetLength;
This gives a different error

Code:
O3DMVector.cpp:5: error: expected unqualified-id before ‘(’ token
Anyone know what I have to do to get this working?
Reply With Quote

Sponsored Links