There is a universal function start , which takes as input a pointer to an unknown function that must be executed and an array of unknown parameters, one of which must be passed to this function as input, that is:

 typedef void(*func_t)(void*); 

This universal function (or a method of a certain class) knows nothing about the obtained function or data array, except for one thing - which element of the array should be passed to the obtained function as input.

Tell me how it can be done using 2 parameters, i.e .:

 void MyFunc(void* params) { CMyParams* data = (CMyParams*)params; // основной код } CMyParams* myAllData = new CMyParams[1000]; start(MyFunc, myAllData); 

Where

 void start(func_t func, void* data) { const int index = 123; // единственный параметр о котором знает функция start func(&data[index]); } 

And with this way of passing the parameters, I have a problem, but my head doesn’t cook something. Probably it should pop up void** , but do not catch :(

I solved this problem by passing the third index parameter to the func_t function, but this is a pure crutch. And how to do without it?

And I want the start function not to know anything about the function and the data it receives at all - not her business.

PS

The fact is that, in addition to index , the start function also knows about the amount of the transferred amount array and must not just call the function, but call a function with different parameters (i.e., in a loop)

 for (index = 0; index < amount; index++) { func(&data[index]); } 
  • one
    Well, pass in start immediately a pointer to the 123rd object. - VTT
  • Do you intentionally complicate things? .. You can just call MyFunc (& myAllData [123]). And the start function is absolutely not needed. Instead of void *, define a function with the argument CMyParams *. Pass the pointer to the array to the function, make sure that this array has more than 123 elements, and then ensure that the array is deleted ... Not for our century, this approach is AR Hovsepyan
  • Oh, I forgot to write an important thing - the fact is that the start function, besides index , also knows about the amount array being transferred, and should not just call the function, but call the function with different parameters (i.e. in a loop) - Zhihar

1 answer 1

It seems to me that this is not possible.

In your example, you ask to refer to an element of the data array (of type void *) with a specific index:

 for (index = 0; index < amount; index++) { func(&data[index]); } 

But the compiler cannot do this for one simple reason. For example, referring to a certain mas [4], the compiler must know where to move (by how many bytes from the beginning of the array), performing the mas operation [4]. If mas is declared as (as an example) int mas [10], the compiler knows the data type, it can calculate the size of one element and can move by the necessary number of elements.

The address mas [4] is equal to the address of the beginning of the array mas + sizeof (int) * 4, where it takes information about the int from the declaration.

Unfortunately, in the case of cc void *, the compiler cannot get the size of the data element.