fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Base
  5. {
  6. void foo() const {
  7. cout << "Base::foo()\n";
  8. }
  9.  
  10. virtual void virt() const {
  11. cout << "Base::virt() called\n";
  12. }
  13. };
  14.  
  15. struct Derived : private Base
  16. {
  17. const Base* get() const {
  18. return this;
  19. }
  20.  
  21. private:
  22. virtual void virt() const override {
  23. cout << "Derived::virt() called\n";
  24. }
  25. };
  26.  
  27. int main()
  28. {
  29. Derived d;
  30. //const Base *b = &d; //uncomment to get error
  31. const Base *b = d.get();
  32. b->foo();
  33. b->virt();
  34. }
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
Base::foo()
Derived::virt() called