Thread: So I decided to use function pointers in my code for the first time...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Question So I decided to use function pointers in my code for the first time...

    So I decided to use function pointers to class member functions in a templated function in my code for the first time and my code compiled and worked exactly as I wanted it to first try. That's never happened before. It's just not right. There's supposed to be a bunch of crap happening when you try something with no prior knowledge of it.


    This must mean that I'm going to encounter some nasty run time bug to make up for this.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you have a problem, post your code!

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    That's the problem...I don't have a problem!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Obviously you do, or you wouldn't have made a topic
    Even if your code compiles and you suspect something nasty is happening, you have a problem because you're entrusting us to help you!
    So... post your code! Post a snippet that works, or whatever. How can we know what to help you with otherwise?

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    So it seems to good to be true? You've compiled code before, maybe this success was just a freak thing; unexpected success isn't always fishy.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by A10 View Post
    So I decided to use function pointers to class member functions in a templated function in my code for the first time and my code compiled and worked exactly as I wanted it to first try. That's never happened before. It's just not right. There's supposed to be a bunch of crap happening when you try something with no prior knowledge of it.
    Cool!

    And yet from the way your describe, it sounds like you could have solved this problem using some more templates instead of member function pointers. Care to explain what sort of code this is and what you're doing? Just out of curiosity.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Cool!

    And yet from the way your describe, it sounds like you could have solved this problem using some more templates instead of member function pointers. Care to explain what sort of code this is and what you're doing? Just out of curiosity.
    The Program keeps track of files on my hard drive. (Itunes keeps losing my music so I wanted to create a file manager that I could easily find all my music files on my hard drive faster then windows search utility, now I keep adding functionality to it...It gives me a good project)

    I wrote a function using a shell sort that arranges classes according to the alphabetical order of a certain string that the class contains (like a file name). The problem was that the class has other strings that I wanted to be able to sort alphabetically (file extensions etc) and I have other classes with strings that I wanted to sort alphabetically. Instead of copying and pasting the function 8 times with only 1 or two small modifications each time, I pass a templated vector of pointers to a class (vector<T*>), and a member function pointer to the get function of that class that returns a string(string(T::*fgetString)()const). It's worked with everything I've thrown at it so far.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    "If at first you do succeed, try not to look astonished".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    I like to use function pointers to make arrays of functions

    Code:
    for ( int x = 0; x < upper_bound; x++ )
        myFunction [x] ( );
    It's lots of fun

    Unfortunately I have yet to find a practical use for arrays of functions
    My Website

    "Circular logic is good because it is."

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perfect when you have a function to call dependant on data.
    Opcodes in emulators are a good example. Another if you want to call a function depending on what value a variable is (like a calculator).

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by DavidP View Post
    I like to use function pointers to make arrays of functions

    Code:
    for ( int x = 0; x < upper_bound; x++ )
        myFunction [x] ( );
    It's lots of fun

    Unfortunately I have yet to find a practical use for arrays of functions
    In a driver, it's quite often used to represent similar but not identical function for different device types or functions - typically the "bitblt()" has a parameter controlling the way that bits are combined between the src and the destination. There are 256 different variations, most of which are sort of similar but not quite. A table that points to a function to perform the most common (optimized variations) or NULL for "have to do it 'by hand'" is an option there.

    --
    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.

  12. #12
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> I like to use function pointers to make arrays of functions
    I did the same. Completely useless* I'll agree, but fun none the less. Guess it's good to know... if you're making a driver ... or want to impress some ladies.

    *for the purposes of where I used it.

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by twomers View Post
    >> I like to use function pointers to make arrays of functions
    I did the same. Completely useless* I'll agree, but fun none the less. Guess it's good to know... if you're making a driver ... or want to impress some ladies.

    *for the purposes of where I used it.
    For example class methods are based on arrays of function pointers.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Class function pointers
    By VirtualAce in forum C++ Programming
    Replies: 40
    Last Post: 02-17-2005, 12:55 AM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM