Skip to main content
gamedev.net
🔒 Locked

Function Pointers

Started by Yaskil 4 replies 750+ views
Original Post
Yaskil
Yaskil
Hi all, i need to retrieve the function pointer of non-static member functions but i fail . so let me explain the problem with a few lines of codes. typedef void (*MyFunc)() class CMyClass { public: void NonStaticMember(); static void StaticMember(); }; MyFunc UtulityFunction; CMyClass A; so next line is working. because the function is static and its address is given staticly by start of the application. UtulityFunction = A.StaticMember; // no problem but i need to do the same operation for non static member. it fails. failure is mentally. when an instance created each time the adresses of functions are changing dynamically for each object is it right?... son when i read the reference of c++ programming next sentence is said "For C++, such a pointer can be used to access static member functions. Pointers to class members must use pointer-to-member operators" accordingly that type of operation can be done with static_cast operator. i tried to write next line in the scope of these explanation UtulityFunction = static_cast< MyFunc >(A.NonStaticMember) but it fails either. how can i do it ? [edited by - yaskil on February 19, 2003 2:22:06 AM]
Life is beautiful
LordShade
LordShade
You can't. When you call a non-static member function there is an implicit 'this' pointer passed to the function. How would the function know what data it is operating on if you omitted this?

Even when you use a static function, EVERY time you call it you should be passing a 'this' pointer to it to identify it's operating parameters. If you are not, why make it a static member of a class in the first place? Much more useful to put it out in a GeneralFunc file if this is the case.

Why do you need to store a pointer to the function of a class? Why not just store a pointer to the class itself?

[edited by - LordShade on February 19, 2003 2:42:00 AM]
Qw3r7yU10p!
Qw3r7yU10p!
A pointer-to-static-member function is different to a pointer-to-member function.

function-pointer.org is a useful link. Here''s exactly what you want section 2.1, see 2.5 as well.
Sneftel
Sneftel
quote:
Original post by LordShade
Even when you use a static function, EVERY time you call it you should be passing a ''this'' pointer to it to identify it''s operating parameters. If you are not, why make it a static member of a class in the first place? Much more useful to put it out in a GeneralFunc file if this is the case.



You miss the point of static functions (if there''s a particular object that a static function operates on, it shouldn''t be static [except to satisfy old C APIs]). What about this:


class Foo {
public:
static Foo* GetDominantFoo();
static string FooStyleName(const string& name);
}


These are two good examples of what static members are good for. It is entirely possible for functions to specifically apply to a class, rather than an object. Both of these COULD be moved out of the class, but to do so would be very bad style.


Don''t listen to me. I''ve had too much coffee.
Yaskil
Yaskil
quote:

LordShade:
Why do you need to store a pointer to the function of a class? Why not just store a pointer to the class itself?



In my input system I store the functions which resolves the input messages (like WNDPROC of WinApi) and in the main loop when an input event occurs i broadcast this input event to all registered functions. So this time each function can react to the specific input by itself ... I need to modify my code for OOP aproach. So i need to store the pointers of functions in order to broadcast a new event ..

I have solved the problem. THANKS Everybody my code is below. You can investigate it ...


  
#include <stdio.h>
#include <conio.h>

#include "clinkedlist.h"


CLinkedList g_FonksiyonListesi;

//-----------------------------------------------

//-----------------------------------------------

class CGenel
{
public:
CGenel(){};
void deneme(){};
};

typedef int(CGenel::*Fonksiyon)(int,int);

typedef struct
{
Fonksiyon fonksiyon;
}FONKSIYON;

#define FONKSIYON_DOGRULA(a) reinterpret_cast< Fonksiyon >(a)


//-----------------------------------------------

//-----------------------------------------------

void SistemeFonksiyonEkle( Fonksiyon f_ )
{
FONKSIYON *yeni;

yeni = (FONKSIYON*)malloc(sizeof(FONKSIYON));
yeni->fonksiyon = f_;

g_FonksiyonListesi.AddItem( (void*)yeni, NULL );
}


//-----------------------------------------------

//-----------------------------------------------

class CGemi
{
public:
CGemi()
{
SistemeFonksiyonEkle( FONKSIYON_DOGRULA(this->Topla) );
}
int Topla( int a, int b ){ printf("Gemi.topla\t");return a+b; }
};

//-----------------------------------------------

//-----------------------------------------------

class CUzay
{
public:
CUzay()
{
SistemeFonksiyonEkle( FONKSIYON_DOGRULA(Topla) );
}
int Topla( int a, int b ){ printf("Uzay.topla\t");return a-b; }
};

//-----------------------------------------------

//-----------------------------------------------

class CConi
{
public:
CConi()
{
SistemeFonksiyonEkle( FONKSIYON_DOGRULA(Topla) );
}
int Topla( int a, int b ){ printf("Coni.topla\t");return a*b; }
};


//-----------------------------------------------

//-----------------------------------------------

int main()
{
CGemi Gemi;
CUzay Uzay;
CConi Coni;


g_FonksiyonListesi.Rewind();
FONKSIYON *eleman;
CGenel *nesne;
Fonksiyon a;

void *c = NULL;

do
{
CGenel deneme;

eleman = (FONKSIYON*)g_FonksiyonListesi.GetItem();

a = eleman->fonksiyon;

int b = (deneme.*a)(3,2);
printf("%d\n", b);

free(eleman);
}
while(g_FonksiyonListesi.MoveNext() != NULL );

getch();
return 0;
};


CLinkedList is my own class you should use STL linkedlists instead

in my code next lines are important

  
typedef int(CGenel::*Fonksiyon)(int,int);

This is the definition of function pointer. It is dependent to CGenel class. In order to remove this dependency i have added next line

  
#define FONKSIYON_DOGRULA(a) reinterpret_cast< Fonksiyon >(a)

This line does the cast operation. It is important. If i haven't done such thing, i need to derive the CGemi, CUzay and CConi classes from CGenel class. This line prenvents this. Additionally when invoking each method

  
CGenel deneme;
// get next element from the list ;)

eleman = (FONKSIYON*)g_FonksiyonListesi.GetItem();
a = eleman->fonksiyon;
int b = (deneme.*a)(3,2);
printf("%d\n", b);


in order to invoke a function of a class we need to do it by its instance. so in the next line we fooled up the compiler ...

  
int b = (deneme.*a)(3,2);

//we might use the next lines instead it is working either

CGenel *cl;
int b = (cl->*a)(3,2);


So this is my solution ...
THANKS ANYWAY

[edited by - yaskil on February 19, 2003 7:57:51 AM]
Life is beautiful

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.