Thread: Forceful type cast not working

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Exclamation Forceful type cast not working

    I am writing a program with FLTK and in event handling part wanted to send the address of the handler function to callback function. Type cast just does not wrok.
    Code:
    	btnOpenDB->callback(&FLUI::WndMain::DBOpenBtnClick_scb, reinterpret_cast<void*>(&WndMain::DBOpenBtnClick_cb));
    I want to cast "a pointer to a class member function which accepts one argument" to a void pointer. After all it is just an address. So it is logically possible to cast it. Isn't it? Actually an static_cast should be enough!
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,181
    The problem is that pointers to objects are convertible to pointers to void, but pointers to (member) functions are not convertible to pointers to void.

    Perhaps you could write a function object that serves as a wrapper, then pass a pointer to the function object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Hello laserlight, it was a long time...
    The problem is that pointers to objects are convertible to pointers to void, but pointers to (member) functions are not convertible to pointers to void.
    Is it because of virtual functions? The address of the function I am trying to get is fixed during the lifetime of that class object. I don't see any reason to work around this matter! I want the address (I know I might be saying idiotic words).
    Can you please tell me more about this function object? How can it help?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,181
    Quote Originally Posted by siavoshkc
    Is it because of virtual functions?
    No, it applies to functions in general, whether member or non-member, static or non-static. In general, the conversion to void* is for pointers to objects only (where "object" here is not restricted to objects of class types, e.g., an int is also an object).

    Quote Originally Posted by siavoshkc
    The address of the function I am trying to get is fixed during the lifetime of that class object. I don't see any reason to work around this matter!
    I think it has to do with Harvard architecture in that memory for data and code could well be separate, hence such conversions may be impossible.

    Quote Originally Posted by siavoshkc
    Can you please tell me more about this function object?
    Basically, you can define a class, let's call it Foo, with an overloaded operator() that calls your static member function. The Foo object will store the function pointer as a member, which could be provided as an argument to the constructor. Hence, you can pass a pointer to the object, which can be validly converted to void*, and then cast it back to Foo*, upon which its operator() can be invoked. Of course, you don't even need to overload operator() as a named member function could work as well, but generally this is an accepted approach as the function object could be reused in a context where some template expects a "callable".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    So simply I can create an object which can hold the method address and then give it to callback as the second argument. Sure.

    If FLTK could accept class member addresses there would be no need for this workarounds. Is that possible? callback() should have had an overload to get a thiscall void*() right?

    [edit]
    I miss read your explanation about pointers to objects. I thought you said only member functions cant be dereferenced.

    [edit2]
    Actually thiscall void*(void*) or something...
    Last edited by siavoshkc; 04-03-2015 at 05:39 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,181
    Quote Originally Posted by siavoshkc
    So simply I can create an object which can hold the method address and then give it to callback as the second argument. Sure.
    Yes.

    Quote Originally Posted by siavoshkc
    If FLTK could accept class member addresses there would be no need for this workarounds. Is that possible?
    I think that the whole point of involving void* in the first place is that it cannot anticipate the type of the "payload".

    Quote Originally Posted by siavoshkc
    callback() should have had an overload to get a thiscall void*() right?
    There is no function pointer analogy of void*. A typical solution is to define a template instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Now how can I send address of a member function to my object? The object should be aware of "this" for the member function. Can I define a function pointer which accepts a member function address?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,181
    Quote Originally Posted by siavoshkc
    Now how can I send address of a member function to my object? The object should be aware of "this" for the member function. Can I define a function pointer which accepts a member function address?
    So this is a non-static member function? Yes, you can do this, but you also need to pass and store the WndMain object (or at least a pointer thereof). Search the Web on member function pointers.

    EDIT:
    Then again, why make it so complicated? Why not just store (a pointer to) the WndMain object, then overload operator() to invoke the desired member function of the WndMain object?
    Last edited by laserlight; 04-03-2015 at 06:38 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Quote Originally Posted by laserlight View Post
    So this is a non-static member function? Yes, you can do this, but you also need to pass and store the WndMain object (or at least a pointer thereof). Search the Web on member function pointers.

    EDIT:
    Then again, why make it so complicated? Why not just store (a pointer to) the WndMain object, then overload operator() to invoke the desired member function of the WndMain object?
    What I wanted to do was a nightmare. Well at least now I know about pointer to member functions.

    You mean storing this in an static member pointer? Can you please provide an example? Really sorry to take your time.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,181
    Quote Originally Posted by siavoshkc
    You mean storing this in an static member pointer?
    No, storing the object (or more likely a pointer to the object) in a function object. In such a case, the function object serves as a wrapper for the non-static member function call, i.e., for each separate non-static member function call that you want to pass, you have a different function object class. On the other hand, maybe you could just directly pass a pointer to the WndMain object, I'm not sure.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Type cast clarification
    By Satya in forum C Programming
    Replies: 6
    Last Post: 05-08-2014, 02:55 PM
  2. Is the type cast necessary when using malloc in C?
    By envec83 in forum C Programming
    Replies: 12
    Last Post: 06-16-2011, 09:52 AM
  3. type cast problem
    By tikelele in forum C Programming
    Replies: 10
    Last Post: 10-31-2007, 09:23 PM
  4. type cast to a pointer....
    By rc7j in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2002, 04:13 PM
  5. type cast or conversion?
    By steviecrawf in forum C Programming
    Replies: 2
    Last Post: 11-21-2001, 03:08 PM