fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class PtrHolder
  6. {
  7. private:
  8. char *x_;
  9. public:
  10. PtrHolder(char *x): x_(x) {}
  11. void print(){cout<<"Ptr is "<<*x_<<endl;}
  12. };
  13.  
  14. class RefHolder
  15. {
  16. private:
  17. char &x_;
  18. public:
  19. RefHolder(char &x): x_(x) {}
  20. void print(){cout<<"Ref is "<<x_<<endl;}
  21. };
  22.  
  23. class ValueHolder
  24. {
  25. private:
  26. char x_;
  27. public:
  28. ValueHolder(char x): x_(x) {}
  29. void print(){cout<<"Value is "<<x_<<endl;}
  30. };
  31.  
  32. int main() {
  33. char c = 'a';
  34. PtrHolder p(&c);
  35. RefHolder r(c);
  36. ValueHolder v(c);
  37. c = 'b';
  38. p.print();
  39. cout<<"PtrHolder is "<<sizeof(PtrHolder)<<" bytes"<<endl;
  40. r.print();
  41. cout<<"RefHolder is "<<sizeof(RefHolder)<<" bytes"<<endl;
  42. v.print();
  43. cout<<"ValueHolder is "<<sizeof(ValueHolder)<<" bytes"<<endl; return 0;
  44. }
Success #stdin #stdout 0s 4516KB
stdin
Standard input is empty
stdout
Ptr is b
PtrHolder is 8 bytes
Ref is b
RefHolder is 8 bytes
Value is a
ValueHolder is 1 bytes