Thread: Query On Void Pointers

  1. #1
    Registered User
    Join Date
    Dec 2002

    Question Query On Void Pointers

    Hi Folks,

    I read a statement "Pointers to functions and pointers to members cannot be assigned to void *s" in the book "C++ Programing Language 3rd Edition", Section 5.6, Pointer to Void.

    I believe I have tried to create a similar situation but it works.

    Is my understanding wrong??? or VC++ 6.0 yet to implement this functionality???

    The code that worked is given below.

    //////////////////////////////////////////////////////////// the actual program /////////////////

    #include <iostream>
    // function declarations
    void display();
    void test_void_pointers(void *);
    // function definitions
    void display()
         std::cout << "I am working" << std::endl;
    void test_void_pointers(void *vp)
         // explicit type converstion 
         void (*fp)() = (void (*)()) vp;
         // call display() using pointer to function
    int main()
         // passing the address of the function display.
         // it is being received in a void *vp
        void (*fp)() = display;
        return 0;
    ///////////////////////// end of program
    Last edited by shiv_tech_quest; 01-17-2003 at 04:38 AM.
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Because they don't have to actually be "pointers" in the normal sense of the word. Some compilers may still allow the conversion, but it is not guaranteed to be portable.

    Why aren't they pointers in the normal sense of the word?

    A member function in C++ can be declared as virtual which means that that function calls made through a pointer/reference to the object will be dynamically bound. The single member function pointer has to be capable of polymorphism such that the same member function pointer when applied to pointers to objects of different types with a shared base class will potentially call two different functions (the redefined virtual member function for that particular class/struct/union). The other problem is that a member function can potentially point to a nonvirtual member function as well, which makes tihngs a bit more complicated because now there has to be a way to differentiate between the two, or a way to handle them in the exact same manner.

    In order to do that, the pointer can't just hold the memory location of a function, it often times holds an offset to a v-table and usually even more than that.

    So, even though they are called pointers, they are actually quite different from the word "pointer" when talking about pointers to primitive datatypes. Because of that logical and implementation difference, C++ makes them invalid to typecast to other pointer datatypes.
    Last edited by Polymorphic OOP; 01-14-2003 at 12:27 AM.

  3. #3
    Registered User
    Join Date
    Dec 2002

    The concept seems hard to sink in :(


    Let us assume only the case of normal functions and not member function.

    Functions can be overloaded. As long as my pointer, which is going to make the function call has been declared with the right type and number of arguments, it must have no issues making the call.

    In anycase, we can't make function calls from void pointers. So why can't i initially take the address of that kind of a function (overloaded) into a void pointer and then recast it back to the correct type of pointer that fits the type and number of arguments as that of the overloaded function and then make the call???
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM