Function Pointers.

rifter1818

Well-known member
Joined
Sep 11, 2003
Messages
255
Location
A cold dark place
Well im lost doinging things well outside my current c++/c knowledge but that hasnt stopped me before.
What i want to do is at startup change the code paths of a few different functions, im pretty sure (at least some of them) can be done using function pointers (the how part is what im sketchy on). I know that the DirectXExtensions uses something like this so it can support SSE,3DNow etc.. which is what im trying to do funny enough.
first type of functions would be member functions...
Vector::Add(Vector V1,VectorV2);
....
and i would want to have multiple implementations of this function depending on what cpu is there....
my first thought on it (with my limited knowledge of function pointers) was something like this.
Code:
class Vector
{
friend class FUNCTIONPOINTERALLOCATER; // its init calls change all the function pointers to the correct ones...
protected:
Vector (*ADDDEFAULT)(Vector v1,Vector v2);
public:
Vector Add(Vector v1,Vector v2,Vector (*ADDFUNC)(Vector V1,Vector V2) = ADDDEFAULT);
}
and have the class FUNCTIONPOINTERALLOCATER choose the right function for *ADDDEFAULT....
Second thought came to me instantly thats pretty stupid....
Code:
class Vector
{
friend class FUNCTIONPOINTER....;//
public:
   Vector (*ADD)(Vector v1,Vector v2);
}
again having *ADD pointed to the right function.....
which might work im not sure how efficiant it would be but i cant see it being too bad... (ive been wrong before)
leaving question 2. is there a better way to do this?
and question
3) operators
if i can do it for member functions i can do it to operators right? please say yes.... and how.
Thanks in advance, ive searched the net for almost two days looking at everything i can read on function pointers but all that lead me to was something simular to the first method.

ps
And please dont say why bother D3DX allready has a good vector class. If you want an explination, "You dont have to reinvent the wheel but you should know how the wheel works before you use it" is about my philosiphy on the subject. and there are other uses (besides my quest for knowledge) for such things...
 
Rather quite in here.

Hmm thats rather suspicious as someone here must have the answers....
Anyways my pointing saga continues, ive found a few sites helping me with function pointers but they are so far failing to work, my compiler (VS.net 2003) wants to make all of my function pointers part of the class (not sure the right term here but 1 for each instance of the class aka bad thing (memory inefficiant and have to make constructers set them to point at the right functions therefor also slow....) so i have come up with the following solution (that may be a very generous usage of the term and when this invevitably fails will be edited to read idea...).
Have each class member function call a global function pointer (and make them all inline to avoid extra code usage) (efficiancy is key meaning its a bad idea for me to code it but its too late to change that) this also solves the problem that i couldnt figureout/find a way to make operator pointers (ohh the error descriptions where entertainment though)...
so my suggestion
Code:
//in vector.h
class __declspec(dllexport) VECTOR
{
public:
union
{
float v[4];
struct{float x,y,z,w;};
};
inline void ADD(const VECTOR&);
};
//in vector.cpp
void (*ADDF)(VECTOR& Dest,const VECTOR& L,const VECTOR& R);
void ADDSSE(VECTOR& Dest,cosnt VECTOR& L,const VECTOR& R)
{
....//wouldnt actually bother using sse for addition but its just an example
}
void ADD3DNOW(...)
{
...
}
inline void VECTOR::ADD(const VECTOR& r)
{
     ADDF(this,this,r);
//can i do that pass it as a const and a non constant byref argument?
}
and id have the DllMain() assign ADDF to the proper function...
oh ya by the way this is all in a dll just to make it easier.

Next Question since all of my functions are inline will it work if a inline function inside a dll is called (from outside the dll) and it calls an inline function... oi
1) what will the compiler do
and before you answer that
2) do i want to know...

And of course the really important question will this work? how bad an idea is it?
and of course any pointers on better ways to do this etc would be appreciated.
 
Back
Top