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



--- Sponsor's Message --------------------------------------
Who Are the Top Dogs?
Find out about the best newsletters and discussions!
http://click.topica.com/aaaa4qbUrGczbU68iFb/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
 
 
----- Original Message -----
Sent: Wednesday, March 14, 2001 1:09 AM
Subject: Re: Function-pointers and classes

--- Sponsor's Message --------------------------------------
Discuss Tech News as It Happens!
Keep up with the latest happenings in high tech -- FREE!
http://click.topica.com/aaaa4IbUrGczbU6XK0b/technology
------------------------------------------------------------
This is for you and the other person who needed help on this.
Function pointers and class member pointers are two different entities in c++ (because class members have to deal with polymorfism, etc...)
So you can't assign to a function pointer the adress of a class member but you can assign it to a class member pointer.
(note: member pointers automaticly deal with polymorfism)
It is defined as: type (classname::*pointername)(parameters);
You can use such a pointer using the ".*" and "->*" operators.
Example:
#include <iostream.h>
class c{
public:
 int(c::*xx)(); /* pointer to member*/
 c();
 int x();
 y();
 };
c::c(){}
int c::x()
{
 return -1;
}
c::y()
{
 xx=&c::x;
 return (this->*xx)();
}
void main()
{
 c cc;
 cout<<cc.y(); /*-1*/
}
 
Unfortunatly this things are often not known.
So c++ is great after all :)
-----Original Message-----
From: Christian Gyrling <d98chgy@stud.hh.se>
To: Game Programmers Mailing List <gameprogrammer@topica.com>
Date: Tuesday, March 06, 2001 12:43 PM
Subject: 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
------------------------------------------------------------
Hello all
 
First of all I give credit to all those who are taking control
of this multi-programmer-game.
 
Next I have a problem similar to that oh Josiha Avery. The thing is
how to use a function-pointer to an instance in a class.
 
int CacheSocket::create()
{
// Locally defined function pointer
// This is how the function-pointer should look to be accepted in the CreateThread
DWORD   (__stdcall *func)(void*);
 
// A function defined in the class we are currently in
func = this->ServerThread;            // THIS IS WHERE THE ERROR OCCURS
ERROR MSG
cannot convert from 'unsigned long (__stdcall CacheSocket::*)(void *)'
to 'unsigned long (__stdcall *)(void *)'
 
// Create the thread
CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)func, NULL, NULL, &ThreadId );
}
 
DWORD WINAPI CacheSocket::ServerThread( LPVOID lpParameter )
{
}
 
Please help me with this one
 
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
____________________________________________________________
T O P I C A  -- Learn More. Surf Less. 
Newsletters, Tips and Discussions on Topics You Choose.
http://www.topica.com/partner/tag01
____________________________________________________________
T O P I C A  -- Learn More. Surf Less.
Newsletters, Tips and Discussions on Topics You Choose.
http://www.topica.com/partner/tag01