![]() |
|
#1
|
|||
|
|||
Saving some if statements Howdy y'all! That being said lets continue.Here's the lowdown: In my program there is a function (we'll call it func_main) that is called hundreds of times per second, and that function (func_main) has to then call one of twenty four different functions (we'll say func1 to func24) each time based on current conditions. These current conditions do not change constantly, but only when a specific event takes place. Being that func_main is called hundreds of times per second it is incredibly inefficient to evaluate which function (func1 to func24) to call each time, especially since conditions are only changing about once every 5-10 seconds. So my first thought was: "Maybe I could make a variable that stores or points to a function (func1 to func24). That way func_main won't have to evaluate what function to call, because I can make another special function that sets the variable only when conditions change." Then I thought: "I have no idea how to do that", and finally: "I should post this on a forum full of friendly programming geniuses!". Well here I am! Thanks for all the help you can give me. Any help at all would be greatly appreciated. P.S. Here's an example of what I'm talking about CPP / C++ / C Code:
Thanks again -Tek Last edited by dsmith : 21-Mar-2004 at 16:44.
Reason: Please use [c] & [/c] to highlight c code
|
|
#2
|
||||
|
||||
|
Hello Tek. Welcome to GIDForums. I changed your post to include c highlighting tags. It makes the C-syntax much eaiser to read.
Anyway, it sounds like you want to you use function pointers. These are pretty simple and can actually be a cool way to get rid of if/then trees and case/switch. The following program shows a simple (very simple ) use of function pointers.CPP / C++ / C Code:
You should be able to use the same type of thing in what you want to do. Hope this helps, d |
|
#3
|
|||
|
|||
|
Thanks. I got a bit confused when I tried implementing it because the pointer function is pointing to a member function of this class I wrote, and apparently the way you declare the pointer function is different when it's pointing to a member function. but anyway, thanks!
|
|
#4
|
|||
|
|||
|
Well I thought I had it.... but I don't
I'm extremely confused about pointers to member functions I have something that looks like this: CPP / C++ / C Code:
I've tried many things including: CPP / C++ / C Code:
all of which give me the same error, except the last one, which says "syntax error: '<tag>::*' " Thanks again for any help. I did a bit of research before posting this to see if I could grab a simple answer but it seems that this is a very advanced topic, and I'm still pretty much a beginner, so if you could word your answers in a way someone like me can understand that would be excellent. Thanks! |
|
#5
|
|||
|
|||
|
Well I've always been able to call functions below the contructor function, so I'm pretty sure that isn't the problem. I've been researching this for a while since I posted last, and the more I look the more I realize this is a very ugly topic that hasn't been touched very much. The concept of a pointer to a member function has been reviewed over and over again, however the concept of a member variable being a pointer to a member function has been virtually untouched. I've only found one person who addressed this topic, and what they did doesn't work for me. www.gamedev.net
Thanks for your help, though. -J |
|
#6
|
||||
|
||||
|
I tried compiling your code with my advice taken into consideration, and it still had the same error, so I deleted my post hoping that no one would see it and thereby save myself some embarrassment
I really don't know why something that is possible in C++ would not be covered by anyone. If it's possible, there's documentation on it somewhere. You've just got to look in the right places. Try C++ syntax book maybe. __________________
-Aaron |
|
#7
|
||||
|
||||
|
Hi Tek. Yes doing this with class functions requires different syntax. Try this:
CPP / C++ / C Code:
I had to change a few of the parameters, but this is basically the format that I think you are looking for. Good Luck! d |
|
#8
|
||||
|
||||
|
I'm just a beginner as far as pointers are concerned, but are you supposed to do a delete after you do a new to avoid memory leaks or something?
__________________
-Aaron |
|
#9
|
||||
|
||||
|
Quote:
Yes Aaron. Technically that is correct. However, this is a program that does absolutely nothing useful. It just shows very quickly and very stupidly how to call functions as pointers inside of a class. Any time that you declare a class you should have a constructor and a desctructor which will be called when you do a new and a delete respectively. |
|
#10
|
||||
|
||||
|
I haven't yet learned much about class destructors. All I know is that they are of the format ~classname() and tend to be parameterless. So, are you saying that, technically, the code you wrote above is flawed in that it does not delete dynamically allocated memory space after its through? Does it even matter that you didn't use delete?
__________________
-Aaron |
Recent GIDBlog
Developing GUIs with wxPython (Part 4) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| MySQL Syntax Error | DropZite | MySQL / PHP Forum | 3 | 09-Jul-2003 04:00 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The