fork download
  1. #include <cstdio>
  2.  
  3. //ASM and C way:
  4. struct user { //block of memory
  5. const char* username;
  6. };
  7. void c_name_printer(user* self) {printf("%s\n", self->username);} //function
  8.  
  9. //basically the same as:
  10. //C++ way:
  11. struct name_printer_object { //function object
  12. const char* username;
  13. void operator()() {printf("%s\n", username);} //but is callable like a function
  14. };
  15.  
  16. int main() {
  17. user smith = {"Smith"};
  18. c_name_printer(&smith); //function
  19.  
  20. name_printer_object steve_name = {"Steve"}; //function object
  21. steve_name(); //but callable like a function
  22. }
  23.  
  24. /*The difference is I can pass `steve_name` to another function that expects a "void func()" function,
  25.  and it can call it properly, and will still print "Steve".
  26. The C way I'd _also_ have to pass a pointer to "smith" and the other function would have to provide that
  27. pointer to the c_name_printer pointer. That's just complicated.*/
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
Smith
Steve