Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. class A {
  2. private:
  3. static const int a = 4;
  4. /*...*/
  5. };
  6.  
  7. class A{
  8. private:
  9. static const int a = 4; // valid
  10. static const std::string t ; // can't be initialized here
  11. ...
  12. ...
  13. };
  14.  
  15.  
  16. // in a cpp file where the static variable will exist
  17. const std::string A::t = "this way it works";
  18.  
  19. class X
  20. {
  21. public:
  22. static int i;
  23. };
  24. int X::i = 0; // definition outside class declaration
  25.  
  26. class C {
  27. static int i;
  28. static int j;
  29. static int k;
  30. static int l;
  31. static int m;
  32. static int n;
  33. static int p;
  34. static int q;
  35. static int r;
  36. static int s;
  37. static int f() { return 0; }
  38. int a;
  39. public:
  40. C() { a = 0; }
  41. };
  42.  
  43. C c;
  44. int C::i = C::f(); // initialize with static member function
  45. int C::j = C::i; // initialize with another static data member
  46. int C::k = c.f(); // initialize with member function from an object
  47. int C::l = c.j; // initialize with data member from an object
  48. int C::s = c.a; // initialize with nonstatic data member
  49. int C::r = 1; // initialize with a constant value
  50.  
  51. class Y : private C {} y;
  52.  
  53. int C::m = Y::f();
  54. int C::n = Y::r;
  55. int C::p = y.r; // error
  56. int C::q = y.f(); // error
  57.  
  58. #include <iostream>
  59. using namespace std;
  60.  
  61. struct X {
  62. static const int a = 76;
  63. };
  64.  
  65. const int X::a;
  66.  
  67. int main() {
  68. cout << X::a << endl;
  69. }
  70.  
  71. template<class T> struct X{
  72. static T x;
  73. };
  74.  
  75. template<class T> T X<T>::x = T();
  76.  
  77. int main(){
  78. X<int> x;
  79. }
  80.  
  81. // .h
  82. class A{
  83. private:
  84. static const int a = 4;
  85. static const foo bar;
  86. ...
  87. ...
  88. };
  89.  
  90. // .cpp
  91. const foo A::bar = ...;
  92.  
  93. class Entry {
  94. public:
  95. int x;
  96. Entry(v):x(v){}
  97. }
  98.  
  99. struct Type {
  100. static constexpr Entry GetEntry() { return Entry(10); }
  101. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement