|
| 1 | +/* |
| 2 | +# static |
| 3 | +
|
| 4 | +This is about static inside classes. Outside classes, it is just the same as in C. |
| 5 | +
|
| 6 | +In classes, they are just basically globals with a namespace. |
| 7 | +*/ |
| 8 | + |
| 9 | +#include "common.hpp" |
| 10 | + |
| 11 | +class OtherClass { |
| 12 | +public: |
| 13 | + int i; |
| 14 | + // J cannot be set from constructor. |
| 15 | + int j; |
| 16 | + OtherClass(int i) : i(i), j(0) {} |
| 17 | +}; |
| 18 | + |
| 19 | +class Class { |
| 20 | +public: |
| 21 | + // Data. |
| 22 | + |
| 23 | + int i; |
| 24 | + |
| 25 | + static int iStatic; |
| 26 | + |
| 27 | + // Concflicts with static int. |
| 28 | + //int iStatic; |
| 29 | + |
| 30 | + // ERROR: cannot init a non-const like this. |
| 31 | + //static int iStaticInit = 0; |
| 32 | + static int iStaticInit; |
| 33 | + |
| 34 | + // OK: because const static integral type |
| 35 | + const static int iConstStatic = 0; |
| 36 | + |
| 37 | + // ERROR. Not initialized outside of the class. |
| 38 | + //static int iNoInit; |
| 39 | + |
| 40 | + // Object data |
| 41 | + |
| 42 | + static OtherClass otherClass; |
| 43 | + |
| 44 | + // https://stackoverflow.com/questions/1197106/static-constructors-in-c-i-need-to-initialize-private-static-objects |
| 45 | + static OtherClass otherClassWithJ; |
| 46 | + static struct _StaticConstructor { |
| 47 | + _StaticConstructor() { |
| 48 | + otherClassWithJ.j = otherClassWithJ.i + 1; |
| 49 | + } |
| 50 | + } _staticConstructor; |
| 51 | + |
| 52 | + // Static methods |
| 53 | + |
| 54 | + // Must be defined later. |
| 55 | + static int staticMethod(); |
| 56 | + |
| 57 | + // Unlike data, we can define the methods here, just like for global functions. |
| 58 | + static int staticGet1() { |
| 59 | + return 1; |
| 60 | + }; |
| 61 | + |
| 62 | + // ERROR: no this in static. |
| 63 | + //static int staticMethodThis() { return this->i; }; |
| 64 | + |
| 65 | + // Can use static variables in static methods. |
| 66 | + static int doubleIStatic() { |
| 67 | + return iStatic + Class::iStatic; |
| 68 | + }; |
| 69 | + |
| 70 | + // ERROR: static cannot be const. That would make no sense, since const is all about this. |
| 71 | + //static void staticMethodConst() const; |
| 72 | + |
| 73 | + // Non static methods |
| 74 | + |
| 75 | + // Access static variable and method from member method. |
| 76 | + int doubleIStaticPlus1NotStatic() { |
| 77 | + return iStatic + this->iStatic + staticGet1(); |
| 78 | + } |
| 79 | + |
| 80 | + // Set static variable from member method. |
| 81 | + void setIStaticNotStatic(int i) { |
| 82 | + iStatic = i; |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +// This is mandatory on some source file, just like for global variables. Related: |
| 87 | +// https://stackoverflow.com/questions/20488579/why-we-declare-static-variable-in-a-class-the-definition-in-outside-of-the-cla |
| 88 | +int Class::iStatic; |
| 89 | + |
| 90 | +int Class::iStaticInit = 0; |
| 91 | + |
| 92 | +int Class::staticMethod() { return 0; } |
| 93 | + |
| 94 | +OtherClass Class::otherClass(1); |
| 95 | +// ERROR |
| 96 | +//Class::otherClass.inc(); |
| 97 | + |
| 98 | +OtherClass Class::otherClassWithJ(1); |
| 99 | +// Must come after all statics. |
| 100 | +Class::_StaticConstructor Class::_staticConstructor; |
| 101 | + |
| 102 | +int main() { |
| 103 | + // Basic static field example. |
| 104 | + { |
| 105 | + Class c, c1; |
| 106 | + int i; |
| 107 | + c.iStatic = 0; |
| 108 | + assert(Class::iStatic == 0); |
| 109 | + c1.iStatic = 1; |
| 110 | + assert(Class::iStatic == 1); |
| 111 | + Class::iStatic = 2; |
| 112 | + assert(Class::iStatic == 2); |
| 113 | + c.setIStaticNotStatic(3); |
| 114 | + assert(Class::iStatic == 3); |
| 115 | + assert(Class::otherClass.i == 1); |
| 116 | + assert(Class::otherClassWithJ.i == 1); |
| 117 | + assert(Class::otherClassWithJ.j == 2); |
| 118 | + //assert(Class::iNoInit == 0); |
| 119 | + } |
| 120 | + |
| 121 | + // Basic static method example. |
| 122 | + { |
| 123 | + Class c; |
| 124 | + assert(c.staticMethod() == 0); |
| 125 | + assert(c.doubleIStaticPlus1NotStatic() == 2 * Class::iStatic + 1); |
| 126 | + assert(Class::staticMethod() == 0); |
| 127 | + assert(Class::staticGet1() == 1); |
| 128 | + assert(Class::doubleIStatic() == 2 * Class::iStatic); |
| 129 | + } |
| 130 | +} |
0 commit comments