Skip to content

Commit 21adfc5

Browse files
committed
C: split globals, C++: split static for classes
1 parent 647dc89 commit 21adfc5

7 files changed

Lines changed: 176 additions & 110 deletions

File tree

‎c/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
1. return
7171
1. [No return non-void](no_return_non_void.c)
7272
1. [_Noreturn](noreturn.c)
73+
1. [Global scope](global.c)
7374
1. [Operator](operator.c)
7475
1. [sizeof()](sizeof.c)
7576
1. [Sequence point](sequence_point.c)

‎c/global.c‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
# global scope
3+
4+
What you can or can't do on global scope.
5+
*/
6+
7+
#include "common.h"
8+
9+
/* This is a global variable: can be accessed and modified everywhere */
10+
int global = 1;
11+
int constGlobal = 1;
12+
13+
/* OK, const. Operators are magic functions. */
14+
int global2 = 1 + 1;
15+
16+
/* ERROR: only var declarations with const initialization allowed. Compare to C++. */
17+
18+
int ret1() {
19+
return 1;
20+
}
21+
22+
/* Non-const. */
23+
/*int global2 = global + 1;*/
24+
/*int global2 = constGlobal + 1*/
25+
26+
/* Funcion call. */
27+
/*puts("asdf");*/
28+
/*int global3 = ret1();*/
29+
30+
/* ERROR Cannot create scopes or branch in global scope. */
31+
32+
/*{}*/
33+
/*if(1){}*/
34+
35+
int main(void) {
36+
assert(global == 1);
37+
assert(constGlobal == 1);
38+
assert(global2 == 2);
39+
return EXIT_SUCCESS;
40+
}

‎c/main.c‎

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,7 @@ This file is being split up into smaller parts and will eventually disappear.
66

77
#include "common.h"
88

9-
/* # global scope */
10-
11-
/* This is a global variable: can be accessed and modified everywhere */
12-
int global = 1;
13-
14-
/* OK! */
15-
int global2 = 1+1;
16-
17-
int ret1() {
18-
int i;
19-
/* before main! */
20-
return 1;
21-
}
22-
23-
/* ERROR: only var declarations with const initialization allowed */
24-
25-
/* Non-const. */
26-
/*int global2 = global+1;*/
27-
28-
/* Funcion call. */
29-
/*puts("asdf");*/
30-
/*int global3 = ret1();*/
31-
32-
/* Branching statement. */
33-
/*if(1){}*/
34-
35-
/* ERROR */
36-
/* Cannot create scopes here like that */
37-
/* They'd be useless anyways. */
38-
39-
/*{}*/
40-
41-
int same_name_as_variable() { return 0; }
42-
439
int main() {
44-
45-
/* # variables */
46-
{
47-
{
48-
int i;
49-
i = 5;
50-
}
51-
52-
{
53-
int i = 5;
54-
int j = 7;
55-
}
56-
}
57-
5810
/*
5911
# Overflow
6012

‎cpp/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
1. [Implicitly deleted constructor](implicitly_deleted_constructor.cpp)
3838
1. [Constructor init object member](constructor_init_object_member.cpp)
3939
1. [Initializer list constructor](initializer_list_constructor.cpp)
40+
1. [static](static.cpp)
4041
1. Inheritance
4142
1. [Inheritance override](inheritance_override.cpp)
4243
1. [Inherit overload_hide](inherit_overload_hide.cpp)

‎cpp/class.cpp‎

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,6 @@ class Base {
289289
void method() {
290290
callStack.push_back("Base::method()");
291291
int i = iAmbiguous;
292-
i = iStatic;
293-
i = iConstStatic;
294292
}
295293

296294
void constMethod() const;
@@ -326,30 +324,12 @@ class Base {
326324
<http://stackoverflow.com/questions/634662/non-static-const-member-cant-use-default-assignment-operator>
327325
*/
328326

329-
// # static
330-
331-
static void staticMethod();
332-
333-
// ERROR: static cannot be const.
334-
//static void staticMethod() const;
335-
336-
static int iStatic;
337-
338-
// Concflicts with static int.
339-
//int iStatic;
340-
341-
// OK: because const static integral type
342-
const static int iConstStatic = 0;
343-
344327
/*
345328
# member initialization outside of constructor
346329
*/
347330

348331
const int iConstInit = 0;
349332

350-
// ERROR: cannot initialize here unless const.
351-
//static int iStatic = 0;
352-
353333
// ERROR: non-integral type.
354334
//const static float fConstStatic = 0.0;
355335

@@ -528,9 +508,6 @@ void Base::constMethod() const {
528508
*/
529509
this->mutableI = 1;
530510

531-
// Static changes can still be done.
532-
this->iStatic = 1;
533-
534511
callStack.push_back("Base::constMethod()");
535512

536513
{
@@ -553,22 +530,6 @@ void Base::constMethod(int *& ip) const {
553530
//ip = &this->iPrivate;
554531
}
555532

556-
int Base::iStatic = 0;
557-
558-
void Base::staticMethod() {
559-
callStack.push_back("Base::staticMethod()");
560-
561-
// ERROR: no this!
562-
//int i = this->i;
563-
564-
// OK: ok to use static vars
565-
int i = iStatic;
566-
}
567-
568-
// ERROR: static linkage, like in C static.
569-
// meaning func only visible from current translational unit
570-
//static void staticMethod()
571-
572533
// Must come outside
573534
const Member Base::member2 = Member(1);
574535

@@ -1295,26 +1256,6 @@ int main() {
12951256
assert(callStack == expectedCallStack);
12961257
}
12971258

1298-
// # static fields
1299-
{
1300-
{
1301-
Class c, c1;
1302-
int i;
1303-
c.iStatic = 0;
1304-
assert(Class::iStatic == 0);
1305-
c1.iStatic = 1;
1306-
assert(Class::iStatic == 1);
1307-
Class::iStatic = 2;
1308-
assert(Class::iStatic == 2);
1309-
}
1310-
1311-
{
1312-
Class c;
1313-
c.staticMethod();
1314-
Class::staticMethod();
1315-
}
1316-
}
1317-
13181259
/*
13191260
# temporary objects
13201261

‎cpp/constexpr.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,21 @@ class MyClass {
6262
public:
6363
MyClass() : i(1) {}
6464
constexpr MyClass(int i) : i(i) {}
65-
// ERROR: Nah.
65+
// ERROR: Nah, no overload.
6666
//MyClass(int i) : i(i) {}
6767
int i;
68+
static int member;
6869
constexpr int f(int j) const { return this->i + j; }
6970
constexpr int noThis(int j) const { return j + 1; }
7071
int nonConst() const { return std::time(NULL); }
72+
// ERROR: constexpr data must be static, or this wouldn't make much sense / be useful, right?
73+
//constexpr int nonStaticConstexpr;
7174
};
7275

73-
7476
constexpr int ConstexprFactorial(int n) {
7577
return (n == 1) ? 1 : n * ConstexprFactorial(n - 1);
7678
}
7779

78-
7980
/*
8081
ERROR: the compiler ensures that the function return is constexpr,
8182
so this does not compile.

‎cpp/static.cpp‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)