Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created July 14, 2017 02:05
Show Gist options
  • Save kaityo256/a9f004b087307906e636529fe479d3dc to your computer and use it in GitHub Desktop.
Save kaityo256/a9f004b087307906e636529fe479d3dc to your computer and use it in GitHub Desktop.
A sample using ternary operators
#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);
}
@kaityo256
Copy link
Author

$ g++ test5.cpp
$ ./a.out
-----
My class is Super*
I am Super.
-----
My class is Super*
I am Sub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment