fork download
  1. #include <iostream>
  2. using namespace std;
  3. class Base
  4. {
  5. public:
  6. virtual Base* clone() { return new Base(*this); }
  7. virtual void identify() {std::cout << "Base\n"; }
  8. };
  9.  
  10. class Derived : public Base
  11. {
  12. public:
  13. virtual Derived* clone() override { return new Derived(*this); }
  14. virtual void identify() override {std::cout << "Derived\n"; }
  15. };
  16.  
  17. int main()
  18. {
  19. Base* foo = new Derived;
  20. Base* bar = foo->clone();
  21. bar->identify();
  22. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
Derived