Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct A {
- struct B {
- int param1;
- bool param2;
- };
- int param1;
- bool param2;
- inline static B b; // inline variable works only with C++17
- };
- struct C {
- static inline A a;
- int param1;
- bool param2;
- };
- int main () {
- std::cout << "test for static struct inside any struct" << std::endl;
- A a1;
- A a2;
- std::cout << &a1.param1 << std::endl;
- std::cout << &a2.param1 << std::endl;
- std::cout << (&a1.param1 == &a2.param1 ? "Same address" : "Not the same address") << std::endl;
- std::cout << &a1.b.param1 << std::endl;
- std::cout << &a2.b.param1 << std::endl;
- std::cout << (&a1.b.param1 == &a2.b.param1 ? "Same address" : "Not the same address") << std::endl;
- // ================================= //
- std::cout << std::endl;
- std::cout << "static struct inside static struct" << std::endl;
- C c1;
- C c2;
- std::cout << &c1.param1 << std::endl;
- std::cout << &c2.param1 << std::endl;
- std::cout << (&c1.param1 == &c2.param1 ? "Same address" : "Not the same address") << std::endl;
- std::cout << &c1.a.param1 << std::endl;
- std::cout << &c2.a.param1 << std::endl;
- std::cout << (&c1.a == &c2.a ? "Same address" : "Not the same address") << std::endl; // C::a - static
- std::cout << (&c1.a.b == &c2.a.b ? "Same address" : "Not the same address") << std::endl; // C::a::b
- std::cout << &c1.a << std::endl;
- std::cout << (&C::a.b == &a1.b ? "This also obviously works" : "Not the same address") << std::endl; // different syntax test
- // ================================= //
- std::cout << std::endl;
- std::cout << "correct syntax" << std::endl;
- std::cout << C::a.b.param1 << std::endl;
- C::a.b.param1 = 10;
- std::cout << C::a.b.param1 << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement