Created
July 14, 2017 02:05
-
-
Save kaityo256/a9f004b087307906e636529fe479d3dc to your computer and use it in GitHub Desktop.
A sample using ternary operators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <string> | |
| #include <typeinfo> | |
| #include <cxxabi.h> | |
| struct Super{ | |
| virtual void hello(void){ | |
| printf("I am Super.\n"); | |
| } | |
| }; | |
| struct Sub : public Super{ | |
| void hello(void){ | |
| printf("I am Sub.\n"); | |
| } | |
| }; | |
| void | |
| test(int a, int b){ | |
| auto *c = (a > b? new Sub() : new Super()); | |
| const std::type_info& id = typeid(c); | |
| int stat; | |
| char *class_name = abi::__cxa_demangle(id.name(),0,0,&stat); | |
| printf("-----\n"); | |
| printf("My class is %s\n",class_name); | |
| c->hello(); | |
| free(class_name); | |
| delete c; | |
| } | |
| int | |
| main(void){ | |
| test(1,2); | |
| test(2,1); | |
| } |
Author
kaityo256
commented
Jul 14, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment