fork download
  1. struct Base {
  2. virtual void foo() = 0;
  3. };
  4.  
  5. struct Derived : Base {
  6. void foo() override{};
  7. };
  8.  
  9. struct Thing {
  10. Thing(Base *b)
  11. : b(b) {}
  12. Base *b;
  13. void call_foo() const {
  14. b->foo(); //why do I not get an error? b is const and should not be able to call Base::foo
  15. }
  16. };
  17.  
  18. int main() {
  19. Derived d;
  20. const Thing t(&d);
  21. t.call_foo();
  22.  
  23. const Base *b = &d;
  24. //b->foo(); //this correctly says error: passing 'const Base' as 'this' argument discards qualifiers [-fpermissive]
  25. }
  26.  
Success #stdin #stdout 0s 3452KB
stdin
Standard input is empty
stdout
Standard output is empty