0

I am having trouble passing a function as an argument to another function. I know this has been addressed already here, but I would appreciate a little more help because I'm having no success. I have tried to adapt this example to my needs:

C++:How to pass reference-to-function into another function?

I'm trying to make my code more readable. I have a huge amount of code and a lot of it is repetitive. For example:

CreateDatabaseEntry( A_key, name_vector, name_vector2, dimension, "_mySuffix" );
CreateDatabaseEntry( B_key, name_vector, name_vector2, dimension, "_mySuffix" ); ...

Let's say I have 26 of these calls in my current main file, one each for B_key, C_key, etc. I would like to move this kind of stuff to functions in a separate "helper" file (helper.cpp/h). For example, into CreateDatabaseEntries().

Problem

Here's my problem. I would have to pass CreateDatabaseEntry into CreateDatabaseEntries. Each call to CreateDatabaseEntry takes different arguments (for example, A_key, then B_key, and so on). I have tried using the example at the link I provided above. But I get "no matching function call" errors. My guess is that I would not get errors if I were calling ONLY one CreateDatabaseEntry. Because I could hard-code "A_key" in the definition of CreateDatabaseEntries. The problem seems to be that I can't generalize the definition of CreateDatabaseEntries to take A_key, B_key, C_key, or whatever.

Also, this is just a simple representation. In practice, I would want to create several "umbrella" functions like CreateDatabaseEntries that would take not only arbitrary A_key, B_key,..., but also arbitrary name_vector and other arguments.

I am a rookie and it is possible that I'm totally barking up the wrong tree. If my explanation makes any sense to anyone, maybe there is a completely different way I could accomplish this? If so, it would be great to know about it. Thanks.

Community
  • 1
  • 1
user616199
  • 61
  • 1
  • 7
  • I would rather pass a variable size buffer, together with number of elements, as arguments, if what you want is a variable number of arguments. For example, if you know your function will takes ints, but I don't know how many, you can do: `void myfunction(int* intbuffer, unsigned int how_many_ints)`. I don't know if this is what you want exactly. – m0skit0 Sep 26 '11 at 11:11
  • Thank you. I don't actually know what a buffer is, but I'll look into it! The function myfunction will always take the same number and type of ints/vectors (or whatever). Only the specific ints/vectors are different. – user616199 Sep 26 '11 at 11:12
  • A buffer is just a reserved memory area. For example, if you want say 5 ints: `int* buffer = new int[5]; buffer[0] = 11; buffer[1] = 21; buffer[2] = 23; buffer[3] = 34; buffer[4] = 534; myfunction(buffer, 5); delete[] buffer;` This way you can pass as many arguments as you want, and they can even be from different types. But I suggest you not abusing this behaviour since it's less readable. Use it only if really no other (better) solution is available. – m0skit0 Sep 26 '11 at 11:17
  • Thanks a lot. Would I have to worry about managing that memory (deleting it, etc)? Would that be the same as using pointers instead of references to do this? Sorry for the dumb question! – user616199 Sep 26 '11 at 11:20
  • AFAIK pointers == references. And yes, you need to free that memory manually, as I did by using delete[], unless you use auto_ptr or something. You can also pass pointers to methods/functions if you like, but IMHO your problem fits better in the solution I proposed you. – m0skit0 Sep 26 '11 at 11:22

1 Answers1

1

Sending the functions seems strange. Parameter passing is indeed better as m0skit0 said.

Why don't you just create an array of parameters, then use a loop to pass each line ? Should be simpler and you don't need to worry about pointers or memory allocation (depending on the parameters and inner mechanics from the function , of course).

imagine this:

int paramList [5] = { 0, 2, 4, 6, 8 };

for (i = 0; i < 5; ++ i)
    myFunc(paramList[i]);

Well obviously in your case you'd make pairs if you have more than 1 parameter... but you get the idea, I think.

If at any point you decide to change the parameter type, you could just use polymorphism on the parameters (or switch to templates), unless you want to keep it all C.

TheNomad
  • 892
  • 5
  • 6