fork(1) download
  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4. struct A;
  5. struct B;
  6. struct Base {
  7. int y;
  8. static std::unique_ptr<Base> create( size_t n );
  9. virtual ~Base() { }
  10. };
  11. struct A : public Base {int a; virtual ~A(){};};
  12. struct B : public Base {int b; virtual ~B(){};};
  13.  
  14. std::unique_ptr<Base> Base::create( size_t n ){
  15. if( n == 1 ) return std::unique_ptr<Base>(new A );
  16. else return std::unique_ptr<Base>(new B );
  17. }
  18.  
  19. int main() {
  20. auto x = static_cast<A*>( Base::create(1).get() );
  21. x->y=1;
  22. x->a=2;
  23. return 0;
  24. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty