fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class B;
  6. void f( const B& b );
  7.  
  8. class B
  9. {
  10. friend void f( const B& b );
  11. int p = 42;
  12. virtual int getP() const { return p; }
  13. };
  14.  
  15. class D : public B
  16. {
  17. virtual int getP() const { return p; }
  18. int p = 100500;
  19. };
  20.  
  21. void f( const B& b )
  22. {
  23. cout << b.getP() << endl;
  24. //cout << dynamic_cast<const D&>(b).getP() << endl; // так уже нельзя
  25. }
  26.  
  27. int main() {
  28. B b;
  29. f( b );
  30.  
  31. D d;
  32. f( d );
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
42
100500