How to Check if the function exists in C/C++

前端 未结 8 2076
失恋的感觉
失恋的感觉 2020-12-01 06:24

Certain situations in my code, i end up invoking the function only if that function is defined, or else i should not. How can i achieve this ?

like:
if (func         


        
8条回答
  •  囚心锁ツ
    2020-12-01 06:56

    use pointers to functions.

     //initialize
     typedef void (*PF)();
     std::map defined_functions;
     defined_functions["foo"]=&foo;
     defined_functions["bar"]=&bar;
     //if defined, invoke it
     if(defined_functions.find("foo") != defined_functions.end())
     {
         defined_functions["foo"]();
     }
    

提交回复
热议问题