fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class IParent
  5. {
  6. public:
  7.  
  8. virtual void foo( ) = 0;
  9. };
  10.  
  11. class Root : public IParent {
  12. private:
  13. virtual void foo( ) override { cout << "foo!"; }
  14. };
  15.  
  16. class Test {
  17. public:
  18. Test( IParent& p ) : m_parent(p) {
  19. m_parent.foo(); // Wieso kann ich hier eine private Methode von Root aufrufen?
  20. }
  21. private:
  22. IParent& m_parent;
  23. };
  24.  
  25. int main()
  26. {
  27. //Ausgeführter Code:
  28. Root root;
  29. Test t( root );
  30. root.foo();
  31. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:30:12: error: ‘virtual void Root::foo()’ is private within this context
   root.foo();
            ^
prog.cpp:13:15: note: declared private here
  virtual void foo( ) override { cout << "foo!"; }
               ^~~
stdout
Standard output is empty