Closed Thread
Results 1 to 4 of 4

Thread: C++ - function pointers as a static data member.

  1. #1
    Join Date
    Oct 2007
    Posts
    538
    Rep Power
    21

    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?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: C++ - function pointers as a static data member.

    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.

  4. #3
    Join Date
    Oct 2007
    Posts
    538
    Rep Power
    21

    Re: C++ - function pointers as a static data member.

    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;

  5. #4
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: C++ - function pointers as a static data member.

    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

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Function Pointers
    By breimer273 in forum C and C++
    Replies: 4
    Last Post: 03-16-2011, 09:30 AM
  2. Function Pointers and Inheritance
    By cod3b3ast in forum C and C++
    Replies: 3
    Last Post: 01-12-2010, 05:37 PM
  3. Function Pointers
    By marwex89 in forum C Tutorials
    Replies: 18
    Last Post: 07-09-2009, 08:45 PM
  4. function pointers
    By porno.shome in forum Pascal and Delphi
    Replies: 5
    Last Post: 07-26-2008, 12:42 AM
  5. Static/protected function question
    By SinXJon in forum C# Programming
    Replies: 1
    Last Post: 02-10-2008, 11:04 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts