From cfb34ec560dd83c47f78ca97edfed59c0103e79a Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Mon, 1 Apr 2024 11:11:27 -0700 Subject: [PATCH] C++ namespace examples --- src/scratch/i_hate_c_plus_plus.cpp | 62 ++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/src/scratch/i_hate_c_plus_plus.cpp b/src/scratch/i_hate_c_plus_plus.cpp index 8589c7fa..e0ceee5f 100644 --- a/src/scratch/i_hate_c_plus_plus.cpp +++ b/src/scratch/i_hate_c_plus_plus.cpp @@ -1,30 +1,60 @@ -struct Bar +static int Static = 5; +namespace NS { - int x; - int y; - int z; + static int staticDataInNS = 99; + struct A + { + static int staticData; + int a = 20; + int b = 30; + void Foo() + { + Static += 1; + staticDataInNS += 1; + staticData++; + a++; + b++; + } + }; + int A::staticData = 123; +} + +struct Resource +{ + int resourceType; }; -struct BarContainer +struct Stack { - Bar *bar; - int bar_count; - int bar_cap; + Resource *resource; }; -struct Foo +struct StackNode { - BarContainer bar; - int a; - int b; - int c; + StackNode *next; + Stack v; +}; + +struct Context +{ + StackNode *entry_stack_first; }; int main(void) { - Bar bar[100] = {0}; - Foo foo_ = {bar, 50, sizeof(bar)/sizeof(Bar)}; - Foo &foo = foo_; + Resource r_ = {0}; + Resource *r = &r_; + Stack s = {r}; + StackNode n_ = {0, s}; + StackNode *n = &n_; + Context c_ = {n}; + Context *context = &c_; + + // evaluate `context.entry_stack_first.v.resource.resourceType == 0xd8` + int x = 0; + + NS::A a = {0}; + a.Foo(); return 0; }