fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Foo
  5. {
  6. const int m_bar; // can be initalized only once
  7. int m_zoo; // must be constructed (default possible) but can be overwritten
  8. public:
  9. Foo(): m_bar(1), m_zoo(2) {} // the constant can never be changed
  10. Foo(int i)
  11. : Foo() {m_zoo=i;} // you can still change in the body already constructed items
  12. Foo(int i, int j) : m_bar(i), m_zoo(j) {} // comprehensive init
  13. Foo(char a) : Foo(a,2) {}; // just delegate to the comprehensive version
  14. void show () { cout<<"bar="<<m_bar<<",zoo="<<m_zoo<<endl; }
  15. };
  16.  
  17. int main() {
  18. Foo a, b(11), c(11,13), d('\r');
  19. a.show(); b.show(); c.show(); d.show();
  20. return 0;
  21. }
Success #stdin #stdout 0s 4360KB
stdin
Standard input is empty
stdout
bar=1,zoo=2
bar=1,zoo=11
bar=11,zoo=13
bar=13,zoo=2