Thread: What is a virtual function pointer?

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    vancouver bc
    Posts
    28

    What is a virtual function pointer?

    I think i understand "function pointer": a pointer that points to a function.

    Function pointer-questions:
    1) Can function pointer point to a static function?
    2) Can function pointer point to a member function?
    3) Can function pointer point to a virtual member function?
    4) Why use function pointer? can i just use if/then to avoid the use of function pointer completely? (because i really am afraid of using function pointer)

    generic-questions:
    1) how to use ' extern "C" ' ? is it necessary ?
    2) Why do we need multiple inheritance?

    --TING

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You may find some of the answers here:
    http://www.google.com/search?q=funct....parashift.com
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    1) Can function pointer point to a static function?
    I do not see why not, so I say yes.

    2) Can function pointer point to a member function?
    3) Can function pointer point to a virtual member function?
    There are pointers to member functions, but they are not really function pointers.

    4) Why use function pointer? can i just use if/then to avoid the use of function pointer completely? (because i really am afraid of using function pointer)
    Take for example qsort(). You certainly cannot just use if statements to replace passing the comparator function as a function pointer. The std::sort() generic algorithm takes a comparator that behaves like a function: so it could be a function pointer or a function object. In that sense, you can avoid function pointers in such cases by using function objects (possibly with template code).

    1) how to use ' extern "C" ' ? is it necessary ?
    Read the FAQ: How to mix C and C++

    2) Why do we need multiple inheritance?
    We do not need multiple inheritance. That said, read Inheritance - multiple and virtual inheritance
    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

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You have to use function pointers to call some function in exe from dll - you cannot link directly, because dll does not knows about your exe...

    Look at any win32 API that uses CallBack functions.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by vart View Post
    You have to use function pointers to call some function in exe from dll - you cannot link directly, because dll does not knows about your exe...
    You can link, I do it all the time. For plugin systems for example it could be useful. For code modularizing/reusing it isn't

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM