Thread: member fn pointer -> fn pointer?

  1. #16
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    It's more a question as a complaint. I know far to less about that stuff to be able to complain.

    Quote Originally Posted by brewbuck View Post
    The problem is that the API is not designed to accept this object pointer. You can't turn a dog into a cat.
    for example the api of the sort algorithm is not designed to accept a member pointer. but there are adapters to make it work (whats what I found out while reading about that things). so cats work at least together with dogs. why stop at this point?

  2. #17
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by CornedBee View Post
    (Actually, usually the designer of the API provided you with an additional void* pointer where you can pass whatever you want, so actually the designer did account for it. That makes it your error for not using this pointer.)
    I's still pretty clue less about that point. could you please fill in the example from my first post in this thread using that void pointer, so the object function gets called?

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by pheres View Post
    for example the api of the sort algorithm is not designed to accept a member pointer. but there are adapters to make it work (whats what I found out while reading about that things). so cats work at least together with dogs. why stop at this point?
    That's because the sort algorithm's comparator is a template parameter. It can be ANY kind of object, not just a function pointer.

    In C, the callbacks registered with an API are usually specified to take a void * which holds "user context." This is where you would store the pointer to the object instance. If the API does not provide such a parameter to the callback, then you are out of luck. The API is badly made.

    EDIT: This is an example of how it would work, if the API did provide you with a context parameter:

    Code:
    int my_callback(void *user_param)
    {
        Foo *f = reinterpret_cast<Foo *>(user_param);
        return f->some_func();
    }
    EDIT EDIT: Whoops, I was using static_cast<> before, which wasn't right.
    Last edited by brewbuck; 10-01-2007 at 03:15 PM.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pheres View Post
    Why are that smart people that are able to design such complex languages and compilers and OS'es not willing to store and process such 3 simple 3-tuple?
    Ok, so you are asking why every single call-back capable call isn't taking three pointers instead of one? Because when the Windows NT design was made, it was 1995 or so. At that time, computers had 64MB of RAM if they were "big memory" and "fully fitted with RAM in all sockets". Saving memory in such a situation by not wasting two longwords worth of space, that most people aren't using anyways, because much of the code at that time was still C, not C++. [And it shouldn't be too different from the Windows 3.1 API that was designed even longer ago, when machines had even less memory].

    Unfortunately, comletely scrapping the current API and replacing it with a new one isn't very likely to happen anytime real soon - because it either means that there would have to be two ways to do just about everything except new functions, or you'd need a "translation layer" - and this would be fine for most things, but some speed critical stuff would then not be quite as fast in the new OS as in the old one. Which means that anyone running either the slower version will complain that "this new API makes my application slower - please take it back".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    translation layer
    yes, that was the word I was looking for.

    Ok, at least I do understand now (a little bit) why things are as they are and why they can not be changed. You have to know that all this is not to apparent for c++ beginners. sorry for that insistent re-asking in this thread. But I can't live in peace if I do things without knowing why. thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  4. Pointer to member
    By Billy in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 02:52 AM
  5. Function pointer to class member?
    By no-one in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2002, 08:23 PM