Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-05-2008, 10:35 AM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 408
Rep Power: 8
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?
__________________
Currently bemused by: LLVM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-06-2008, 09:07 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 501
Last Blog:
Programs Under the Hoo...
Rep Power: 9
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-07-2008, 12:40 PM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 408
Rep Power: 8
G_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura about
Default 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;
__________________
Currently bemused by: LLVM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-07-2008, 09:36 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 501
Last Blog:
Programs Under the Hoo...
Rep Power: 9
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default 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 09:36 PM. Reason: Fixed typo
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT -5. The time now is 09:14 PM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 65%

Ads