fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template<class T>
  6. class IDetachable {
  7. public:
  8. virtual T* detached() const = 0;
  9. };
  10.  
  11.  
  12. class A: public IDetachable<A> {
  13. virtual A* detached() const override {
  14. cout << "some implementation which returns a detached A object" << endl;
  15. }
  16. };
  17.  
  18. class B: public A, public IDetachable<B> {
  19. virtual B* detached() const override {
  20. cout << "some implementation which returns a detached B object" << endl;
  21. }
  22. };
  23.  
  24. int main() {
  25. IDetachable<A> *da = new A;
  26. A *a = da->detached();
  27. IDetachable<B> *db = new B;
  28. A *ba = db->detached();
  29. B *bb = db->detached();
  30. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
some implementation which returns a detached A object
some implementation which returns a detached B object
some implementation which returns a detached B object