Advertisement
Guest User

Untitled

a guest
Mar 18th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. // test.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <memory>
  6. #include <iostream>
  7.  
  8. struct Foo {
  9.     Foo(int o) {
  10.         one = o;
  11.     }
  12.     int one;
  13.     virtual int getOne() { return one; }
  14. };
  15.  
  16. struct Bar : public Foo {
  17.     Bar(int o, int t) : Foo(o) {
  18.         two = t;
  19.     }
  20.     int two;
  21.     virtual int getOne() override { return one * two; };
  22.     int getTwo() { return two; }
  23. };
  24.  
  25. int main()
  26. {
  27.     using namespace std;
  28.  
  29.     shared_ptr<Foo> bar = make_shared<Bar>(1, 2);
  30.     cout << "one " << bar->getOne() << endl;
  31.     cout << "two " << dynamic_cast<Bar*>(bar.get())->getTwo() << endl;
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement