nezvers

C++ function pointer with void argument decoding

Aug 26th, 2020 (edited)
1,394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. /*
  2. Github: https://github.com/nezvers
  3. Youtube: https://www.youtube.com/channel/UCb4-Y0E6mmwjtawcitIAzKQ
  4. Twitter: https://twitter.com/NeZversStudio
  5. */
  6.  
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. typedef struct encode_char {
  12.     const char* text;
  13. }encode_char;
  14.  
  15. typedef struct encode_stats {
  16.     const char* name;
  17.     int age;
  18. }encode_stats;
  19.  
  20. void hello(void *arg){
  21.     encode_char *my_decoder;
  22.     my_decoder = (encode_char*)arg;
  23.     cout << "Hello " << my_decoder->text << "!" << endl;
  24. }
  25.  
  26. void stats(void *arg){
  27.     encode_stats *my_decoder;
  28.     my_decoder = (encode_stats*)arg;
  29.  
  30.     cout << "Name: " << my_decoder->name << endl;
  31.     cout << "Age: " << my_decoder->age << endl;
  32. }
  33.  
  34. int main()
  35. {
  36.     void (*function_pointer1)(void*);
  37.  
  38.     function_pointer1 = hello;
  39.     encode_char char_encoder = {"world"};
  40.     //function_pointer1( (void *) &char_encoder );
  41.  
  42.     void (*function_pointer2)(void*);
  43.     function_pointer2 = stats;
  44.     encode_stats stats_encoder = {"NeZvers", 32};
  45.     //function_pointer2( (void *) &stats_encoder );
  46.  
  47.     void (*function_trigger)(void*);
  48.  
  49.     function_trigger = function_pointer1;
  50.     function_trigger( (void *) &char_encoder );
  51.  
  52.     function_trigger = function_pointer2;
  53.     function_trigger( (void *) &stats_encoder );
  54.  
  55.     return 0;
  56. }
  57.  
Add Comment
Please, Sign In to add comment