fork download
  1. #include <iostream>
  2.  
  3. #define CLASS(name) \
  4.   virtual std::string className() { \
  5.   return #name; \
  6. }
  7.  
  8. class A {
  9. public:
  10. CLASS(A)
  11.  
  12. A(){}
  13. ~A(){}
  14.  
  15. void output() {
  16. std::cout << className() << std::endl;
  17. }
  18. };
  19.  
  20. class B: public A{
  21. public:
  22. CLASS(B)
  23. B(){}
  24. ~B(){}
  25. };
  26.  
  27. int main() {
  28. B b;
  29. b.output(); // This obviously outputs "A" but I would
  30. // like it to output "B" from the base class
  31. // function
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0.02s 2856KB
stdin
Standard input is empty
stdout
B