PDA

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



A10
11-09-2007, 10:10 PM
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.

Elysia
11-09-2007, 10:41 PM
If you have a problem, post your code!

A10
11-09-2007, 11:00 PM
That's the problem...I don't have a problem!

Elysia
11-09-2007, 11:02 PM
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?

Yarin
11-09-2007, 11:18 PM
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.

brewbuck
11-09-2007, 11:19 PM
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.

A10
11-09-2007, 11:58 PM
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.

Salem
11-10-2007, 01:05 AM
"If at first you do succeed, try not to look astonished".

DavidP
11-11-2007, 08:40 PM
I like to use function pointers to make arrays of functions :)



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 :(

Elysia
11-11-2007, 08:42 PM
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).

matsp
11-12-2007, 03:30 AM
I like to use function pointers to make arrays of functions :)



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

twomers
11-12-2007, 01:08 PM
>> 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.

maxorator
11-13-2007, 07:13 AM
>> 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. ;)