Advertisement
Guest User

Untitled

a guest
Feb 13th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | Source Code | 0 0
  1. #include <iostream>
  2.  
  3. struct A {
  4.     struct B {
  5.         int param1;
  6.         bool param2;
  7.     };
  8.  
  9.     int param1;
  10.     bool param2;
  11.  
  12.     inline static B b; // inline variable works only with C++17
  13. };
  14.  
  15. struct C {
  16.     static inline A a;
  17.     int param1;
  18.     bool param2;
  19. };
  20.  
  21.  
  22. int main () {
  23.     std::cout << "test for static struct inside any struct" << std::endl;
  24.     A a1;
  25.     A a2;
  26.  
  27.     std::cout << &a1.param1 << std::endl;
  28.     std::cout << &a2.param1 << std::endl;
  29.  
  30.     std::cout << (&a1.param1 == &a2.param1 ? "Same address" : "Not the same address") << std::endl;
  31.  
  32.     std::cout << &a1.b.param1 << std::endl;
  33.     std::cout << &a2.b.param1 << std::endl;
  34.  
  35.     std::cout << (&a1.b.param1 == &a2.b.param1 ? "Same address" : "Not the same address") << std::endl;
  36.    
  37.     // ================================= //
  38.     std::cout << std::endl;
  39.     std::cout << "static struct inside static struct" << std::endl;
  40.     C c1;
  41.     C c2;
  42.  
  43.     std::cout << &c1.param1 << std::endl;
  44.     std::cout << &c2.param1 << std::endl;
  45.  
  46.     std::cout << (&c1.param1 == &c2.param1 ? "Same address" : "Not the same address") << std::endl;
  47.  
  48.     std::cout << &c1.a.param1 << std::endl;
  49.     std::cout << &c2.a.param1 << std::endl;
  50.  
  51.     std::cout << (&c1.a == &c2.a ? "Same address" : "Not the same address") << std::endl; // C::a - static
  52.     std::cout << (&c1.a.b == &c2.a.b ? "Same address" : "Not the same address") << std::endl; // C::a::b
  53.    
  54.     std::cout << &c1.a << std::endl;
  55.     std::cout << (&C::a.b == &a1.b ? "This also obviously works" : "Not the same address") << std::endl;  // different syntax test
  56.    
  57.     // ================================= //
  58.     std::cout << std::endl;
  59.     std::cout << "correct syntax" << std::endl;
  60.     std::cout << C::a.b.param1 << std::endl;
  61.     C::a.b.param1 = 10;
  62.     std::cout << C::a.b.param1 << std::endl;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement