Thread: A pointer to an object's function as a parameter

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    45

    A pointer to an object's function as a parameter

    hiiiiii!

    I'm going slightly insane here! This is the declaration for SDL thread creation:

    SDL_Thread *SDL_CreateThread( int (*fn)(void *), void *data );

    Based on this and the example I found, I take it this means the first parameter is a pointer to an integer returning function ! Great I thought!

    So I've been trying to pass this function a pointer to an object's function. This object is declared within a namespace !

    This is my latest attempt to make this happen (not exact) :-

    Code:
    void a::myclass::init()
    {
            mythread = SDL_CreateThread( &this->thefunc, NULL );
    }
    
    int a::myclass::thefunc(void *whatever)
    {
            return 1;
    }
    I've tried a few variations but this just looks right to me

    The error I'm now getting is this :-

    cannot convert `int (a::myclass::*)(void*)' to `int (*)(void*)' for argument `1' to `SDL_Thread* SDL_CreateThread(int (*)(void*), void*)'

    Does this mean that a function declared thus will not allow anything within a namespace or an object to be passed to it? Is there anything I can do to get around this? I tried casting it with a (*) and it didn't work (I wasn't surprised lol).

    Thanks in advance people!
    we are one

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    This is where C++ actually hides some of the real stuff of what's really happening under the hood. While you may call a function in an OOP style, such as object.function(), that's not what is really being called. It's really being called in the manner of function(object). This means your function's signature is really not matching what you expect.

    You can make it a static function and that should be ok, but that means you have to most likely rework your class structure.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    Ahhh I see ! Thank you for that explaination... I couldn't work out why it would be any different !

    I was hoping I could avoid making it static and passing a pointer, but I suppose not. At least I know why now, hehe
    we are one

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. function pointer & Thread Parameter :: Multithreading
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 09-11-2002, 08:42 AM