http://GameProgrammer.Com

Programming

GP Mailing List
     Thread Index
     Date Index

ATXGPSIG List
     Thread Index
     Date Index

Google
>

Home

Wise2Food



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Function-pointers and classes



============================================================
"Before myTopica, I was buried in an avalanche of email.
Now, thanks to this free solution, I am on top of my inbox."
Get rescued at
http://click.topica.com/aaabBBbUrGczbU68iFb/register
============================================================
I am afraid you didn't quite understand the syntax of the ".*" / "->*" operators.
On the left side of it is a object / pointer to object which contains the method the pointer is pointing at and on the right side is a pointer to a member function of the class wich is NOT necessary part of the class.
Example to clear things up:
 
#include <iostream.h>
 
class MyClass
{
   public:
   void (MyClass::*mfp)(); /*pointer to member of myclass, part of myclass*/
   void func(){
        cout<<"I am in func."<<endl;
        }
   void start(){
        mfp=&MyClass::func;
        }
};
 
void Create(void *ptr){
     MyClass *obj=(MyClass *)ptr;
     obj->func();
     (obj->*(obj->mfp))(); /*the right way:object on the left,pointer on the right*/
}
 
void main()
{
 MyClass m;
 cout<<"Start pointer to member function test"<<endl;
 m.start();
 Create(&m);
 
 MyClass obj;
 void (MyClass::*mfp1)(); /*pointer to member of myclass, not part of myclass*/
 mfp1=&MyClass::func;
 (obj.*mfp1)();
}
 
By the way I found a way to use a function pointer to point to a member function but it has some major disadvantages:
- you have to declare the member as static (so it dosen't work with most ready-made classes)
- a method cannot be both static and virtual
- a static method dosen't get the "this" pointer (you cannot access any of the object's data (!!!)) (you could pass a pointer to the object as a parameter to the method but you still cannot access any of the private object data)
A static method is considered part of a class only at compilation. There is practicly no conection between the static method and the class at runtime.
So I'd advise to stay clear of this unless you really want to use it.
Anyway here's an example:
 
#include <iostream.h>
 
class c{
public:
 void (*xx)();
 static void x(){
        cout<<"in x"<<endl;
        }
 void d(){
      xx=&x;xx();
      }
 };
 
void main()
{
 c cc;
 cout<<"static test"<<endl;
 cc.d();
 c::x(); /*static method - you don't even need a instance to the class to use it*/
}
 
Cosmin
-----Original Message-----
From: Christian Gyrling <d98chgy@stud.hh.se>
To: gameprogrammer@topica.com <gameprogrammer@topica.com>
Date: Wednesday, March 14, 2001 4:47 AM
Subject: Re: Function-pointers and classes

--- Sponsor's Message --------------------------------------
Who Are the Top Dogs?
Find out about the best newsletters and discussions!
http://click.topica.com/aaaa4qbUrGczbU6XMab/TopDogs
------------------------------------------------------------
Thanx for the info. I solved my first problem like that but now I have another one.
Take a look at the following code.
My problem occured when I was about to create multiple threads in a class.
Because of the CreateThread/beginthread functions I must call them with a plain
static function. ( the Create function ) But I´d like for different functions to be called
each time I started a new thread. Like some kind of thread-launcher.
By always sending the instance as the parameter to the Create-function I could
get a hold of the correct instance of the class but I had to get the specific function
to call in the Create-function.
Aaaaa, I thought. I use a member-function-pointer (.* / ->*) but there was an compilation error.
Could it have something to do with the casting???
If I call a public function in the class from the Create function there is no complaint.
 
int    Create( void *ptr ){
   MyClass     *obj = (MyClass*)ptr;
 
    ptr->*func();        // Ok
 
    ptr->*MemberFunctionPointer();        // Compile Error
} 
 
class MyClass
{
   void    (MyClass::*MemberFunctionPointer)();
 
   void    func();
   
   void    start() {
        MemberFunctionPointer = func;
        CreateThread(.....,..., Create, this,....,.... );
    }
}
 
The red line has an error at compilation that says something like. "Evaluates to int"
 
Many Thanks
/Christian Gyrling
 
 
____________________________________________________________
T O P I C A  -- Learn More. Surf Less.
Newsletters, Tips and Discussions on Topics You Choose.
http://www.topica.com/partner/tag01