fork download
  1. #include <iostream>
  2. using namespace std;
  3. class Txtbin {
  4. public:
  5. const static int ERR_EMPTY_IMAGE = 2;
  6. };
  7. class Test1 {
  8. public:
  9. const int ERR_EMPTY_IMAGE = 2;
  10. Test1() = default;
  11. Test1(int x) : ERR_EMPTY_IMAGE{x} {}
  12. };
  13. class Test2 {
  14. public:
  15. const int ERR_EMPTY_IMAGE;
  16. Test2(int x) : ERR_EMPTY_IMAGE{x} {}
  17. };
  18.  
  19. int main() {
  20. Txtbin a;
  21. int err;
  22. Test1 b;
  23. Test1 b_ouch(9);
  24. Test2 c(5), d(6);
  25.  
  26. switch(err){
  27. case Txtbin::ERR_EMPTY_IMAGE: // no need for an object
  28. std::cerr << "Error: Image is empty\n" << std::endl;
  29. break;
  30. }
  31. switch(err){
  32. case a.ERR_EMPTY_IMAGE: // but the object doesn't bother
  33. std::cerr << "Error: Image is empty\n" << std::endl;
  34. break;
  35. }
  36.  
  37. cout << "Sizeof a: "<< sizeof(a) <<endl; // only static but minimum size is 1.
  38. cout << "Sizeof b: "<< sizeof(b) <<endl; // with non static const
  39. cout << "Test2 c ->"<< c.ERR_EMPTY_IMAGE<<" d->"<< d.ERR_EMPTY_IMAGE<<endl;
  40. cout << "Test1 b ->"<< b.ERR_EMPTY_IMAGE<<" b_ouch->"<< b_ouch.ERR_EMPTY_IMAGE<<endl;
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Sizeof a: 1
Sizeof b: 4
Test2 c ->5 d->6
Test1 b ->2 b_ouch->9