fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class BClass
  5. {
  6. private:
  7. const char code; //how to make const?
  8. public:
  9. BClass(char c) : code(c) {}
  10.  
  11. char getCode() const { return code; }
  12. };
  13.  
  14. class AClass
  15. {
  16. private:
  17. const BClass b0; //how to make const?
  18. const BClass b1; //how to make const?
  19. const BClass* bPtrs[2];//how to make const?
  20. public:
  21. AClass() : b0('x'), b1('y')
  22. {
  23. bPtrs[0] = &b0;
  24. bPtrs[1] = &b1;
  25. }
  26.  
  27. void print(int i) const
  28. {
  29. cout << "char=" << bPtrs[i]->getCode() << endl;
  30. }
  31. };
  32. AClass a;
  33.  
  34. int main()
  35. {
  36. a.print(0); // prints "char=x"
  37. a.print(1); // prints "char=y"
  38. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
char=x
char=y