GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Mar-2004, 16:36
TekiFreek TekiFreek is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Posts: 10
TekiFreek is on a distinguished road
Question

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:
void func_main() // this is being called many many times per second
{
     //Call one of twenty four functions without evaluating current conditions
     //(Perhaps I could call a variable that stores or points to a function)
}

void evaluateCurrentConditions() // this is called relatively few times (when
                                            // conditions change)
{
     //Set a variable to store or point to a specific function
}
As you can see, in this way, many operations are saved. Now, I'm no genius, so I may have overlooked something that can be done with my novice programming skills to save me these operations, so feel free to propose your own solution!

Thanks again
-Tek
Last edited by dsmith : 21-Mar-2004 at 16:44. Reason: Please use [c] & [/c] to highlight c code
  #2  
Old 21-Mar-2004, 17:12
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
#include "stdio.h"

void function1()
{
	printf("1\n");
}

void function2()
{
	printf("2\n"); 
}

void function3()
{
	printf("3\n");
}

int main()
{
	void (*func)();

	func = function1;
	(*func)();
	func = function2;
	(*func)();
	func = function3;
	(*func)();
}	


You should be able to use the same type of thing in what you want to do.

Hope this helps,
d
  #3  
Old 21-Mar-2004, 20:47
TekiFreek TekiFreek is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Posts: 10
TekiFreek is on a distinguished road
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  
Old 21-Mar-2004, 21:49
TekiFreek TekiFreek is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Posts: 10
TekiFreek is on a distinguished road
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:

class player
{
     private:
          void (player::*planeCorrection)(GLfloat); // this points to a function that takes a GLfloat and returns nothing
     public:
          player()
          {
               planeCorrection = z_y; // planeCorrection now points to the private member function, z_y (supposedly)
          }
          SomeFunction()
          {
               planeCorrection( 1.0f ); // this does not work. "term does not evaluate to a function"
          }
          void z_y( GLfloat n )
          {
               // unrelated code
          }
};


I've tried many things including:

CPP / C++ / C Code:
*planeCorrection( 1.0f );
this.*planeCorrection( 1.0f );
(player::planeCorrection)( 1.0f );
(player::*planeCorrection)( 1.0f );

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  
Old 22-Mar-2004, 01:34
TekiFreek TekiFreek is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Posts: 10
TekiFreek is on a distinguished road
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  
Old 22-Mar-2004, 01:41
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 22-Mar-2004, 10:18
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Tek. Yes doing this with class functions requires different syntax. Try this:
CPP / C++ / C Code:

#include "stdio.h"

class player
{	
	private:
		void (player::*planeCorrection)(float);
	public:	
		player()
			{
				planeCorrection = &player::z_y;
			};
		void SomeFunction()
			{
				(this->*planeCorrection)(1.0);
			};
		
		void z_y(float n)
			{
				printf("You called?\n");
				printf("With value: %4.2f\n\n",n);
			};	
			
		void y_z(float n)
			{
				printf("What now?\n");
				printf("Value: %4.2f\n\n",n);
			};
			
		void change()
			{
				planeCorrection = &player::y_z;
			};	
		       
};


int main()
{
	player*	you = new player;

	you->SomeFunction();
	you->change();
	you->SomeFunction();	
	return 0;
}	


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  
Old 22-Mar-2004, 10:42
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 22-Mar-2004, 10:53
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by aaroncohn
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?

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  
Old 22-Mar-2004, 11:08
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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 GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 18:05.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.