D
D
DjTolib2021-07-27 11:55:59
C++ / C#
DjTolib, 2021-07-27 11:55:59

How to call a function depending on the received number, without using conditional operators?

void foo1();
void foo2();

void foo(int n){
  // На вход ф-я гарантированно получает 1 или 2
  // В зависимости от этого нужно вызвать первую или вторую функцию
  // Условные операторы, сравнения, свитч нельзя использовать. 
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2021-07-27
@DjTolib

Throw the addresses of functions into an array, select a function by index.
I suggest you figure out the syntax yourself.

R
rPman, 2021-07-27
@rPman

The decision to you already told - to define functions in an array/map everyone according to value and to cause.
There will be no alternative. The function name does not fall into the final binary according to the standard, it is a temporary entity, which means that by name (forming it for example 'func' + n) it will not work, but there are exceptions, for example, if the required functions are exported (for dll for example), Accordingly, depending on the target OS, use win32 GetModuleHandle/GetProcAddress, for linux, Google suggests dlopen/dlsym.
You can also try to analyze the debugging information that will be saved in the exe file or nearby in the file, of course the code must be built in debug mode.
but it's the scariest shit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question