Guest User

Untitled

a guest
Jun 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. class Foo {
  2. private:
  3. int myInt;
  4. };
  5.  
  6. int Foo::myInt = 1;
  7.  
  8. class Foo {
  9. private:
  10. int myInt;
  11. public:
  12. Foo() : myInt(1) {}
  13. };
  14.  
  15. class Foo {
  16. private:
  17. static const int myInt = 1;
  18. };
  19.  
  20. class Foo {
  21. private:
  22. static int myInt;
  23. };
  24.  
  25. class Foo
  26. {
  27. public:
  28. Foo(void) :
  29. myInt(1) // directly construct myInt with 1.
  30. {
  31. }
  32.  
  33. // works but not preferred:
  34. /*
  35. Foo(void)
  36. {
  37. myInt = 1; // not preferred because myInt is default constructed then assigned
  38. // but with POD types this makes little difference. for consistency
  39. // however, it's best to put it in the initializer list, as above
  40. // Edit, from comment: Also, for const variables and references,
  41. // they must be directly constructed with a valid value, so they
  42. // must be put in the initializer list.
  43. }
  44. */
  45.  
  46. private:
  47. int myInt;
  48. };
Add Comment
Please, Sign In to add comment