Using funcions with dirfferent names

I was programming a "game" and i want to use funcions for the attacs of the different monsters. To make it easyer to program I want to create a funcion with the attak of every monster (for example: if i want to some monster deal 7 to 10 damage random i need a funcion to do this).

This is inside another funcion combat() and I try to use it that way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void combat(char*);
void atakmonster1();

int main(){

combat("atakmonster1");


return 0;}

void combat(char* atakfuncion){

    atakfuncion();
}

void atakmonster1(){printf("this is the funcion of the atac of monster 1");}


With this i want to create funcions for the ataks of monsters 2,3.... and i tought that changing the char variable it woud be usefull to call different funcions. But the compilator says me: called obejct 'atacfuncion' is not a funcion.

My question is how I can do it? how i can have an "unnamed funcion" which I can relplace changing the name of one parameter of my funcion.


p.d: sorry for my english it's so hard for me to exrpess me whith it :P
Last edited on
Pass a function pointer.
1
2
3
4
5
6
7
8
9
10
void combat(void (*atakfuncion)());
void atakmonster1();

int main(){
	combat(atakmonster1);
}

void combat(void (*atakfuncion)()){
    atakfuncion();
}
If your monsters are defined as classes with a common base class then you can use polimorphism.

Another simple approach is the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void combat(const char* atakfuncion)
{
   const size_t N = 10;
   void ( *pf[N] )() = 
   { 
      atakmonster1, atakmonster2, atakmonster3, atakmonster4, 
      atakmonster5, atakmonster6, atakmonster7, atakmonster8, 
      atakmonster9, atakmonster10 
   };
   const char * fnames[N] =
   {   
      "atakmonster1", "atakmonster2", "atakmonster3", "atakmonster4", 
      "atakmonster5", "atakmonster6", "atakmonster7", "atakmonster8", 
      "atakmonster9", "atakmonster10" 
   };

/*
   const char **p = std::find_if( std::begin( fnames ), std::end( fnames ),
                                               [=]( const char *s )
                                               {
                                                  return ( std::strcmp( s, atakfuncion ) == 0 );
                                               } );
  
*/
   size_t i  = 0;
   while ( i < N && std::strcmp( fnames[i], atakfuncion ) != 0 ) ++i;
 
   if ( i != N ) pf[i];
}
Last edited on
Also you can apply std::map. For example

std::map<std::string, void ( * )()> m;

and fill it with some names of functions or monsters and corresponding function pointers. When simply call an appropriate function as

1
2
3
4
void combat( const char* atakfuncion )
{
    m.at( atakfuncion )();
}

Thank you!!! that was so usefull :) But i dont know what's that "std::" :S
Last edited on
Are you using directive

using namespace std;

?
Last edited on
std:: is a namespace.
http://www.cplusplus.com/doc/tutorial/namespaces/

All Standard Template Library (STL) containers are in the std:: namespace.

To use the map container vlad referred to you need the following include:
 
#include <map>  


You can also use the following statement to avoid having to prefix every map reference with std::
 
using namespace std;


The advantage of the using statement is brevity, but the disadvantage is that names in the std namespace may conflict with names in your program. For small programs, this isn't a problem, however for large programs, this can be an issue.
Topic archived. No new replies allowed.