开发者

Pass a function as an argument to another function, by reference (C++ rookie)

开发者 https://www.devze.com 2023-04-08 06:55 出处:网络
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 t

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 wo开发者_如何学Culd be great to know about it. Thanks.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号