Advertisement
decyg12

Untitled

Jul 16th, 2021
1,504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. /* FILE: main.cpp (translation unit 1) */
  2.  
  3. #include <iostream>
  4. #include "test.h"
  5.  
  6. void func1(void)
  7. {
  8.     // static Variablen werden einmal beim Programmstart angelegt (und initialisiert),
  9.     // das hei�t der Wert bleibt au�erhalb vom Scope erhalten (siehe output unten).
  10.     static int var = 5;
  11.     std::cout << ++var << std::endl;
  12. }
  13.  
  14. void func2(void)
  15. {
  16.     // Hier wird jedes mal wenn das Programm diese Funktion ausf�hrt die Variable neu angelegt und initalisiert.
  17.     // Der Wert bleibt au�erhalb vom Scope NICHT erhalten (siehe output unten).
  18.     int var = 5;
  19.     std::cout << ++var << std::endl;
  20. }
  21.  
  22. // zuerst test.h lesen (weiter unten)
  23. void test_namespace::print_tu1(void)
  24. {
  25.     std::cout << "Translation unit 1: " << std::endl;
  26.     test_namespace::value = 75;     // Wert wird nur in diesem Tranlation unit ver�ndert.
  27.     std::cout << "Value: " << test_namespace::value << std::endl;
  28.     std::cout << "Address of value: " << &test_namespace::value << std::endl;
  29.     std::cout << std::endl;
  30. }
  31.  
  32. int main()
  33. {
  34.     std::cout << "Call to func1 (static): " << std::endl;
  35.     func1();
  36.     func1();
  37.     func1();
  38.     std::cout << std::endl;
  39.  
  40.     std::cout << "Call to func2 (non static): " << std::endl;
  41.     func2();
  42.     func2();
  43.     func2();
  44.     std::cout << std::endl;
  45.  
  46.     test_namespace::print_tu1();
  47.     test_namespace::print_tu2();
  48.    
  49.     return 0;
  50. }
  51.  
  52. /* FILE: test.h */
  53.  
  54. #pragma once
  55.  
  56. #include <iostream>
  57.  
  58. namespace test_namespace
  59. {
  60.     // static kann auch noch eine andere Bedeutung haben, n�mlich unterdr�ckt es die ODR (One Definition Rule).
  61.  
  62.     // W�rde ich jetzt eine variable
  63.     // int value = 10;
  64.     // global anlegen, wird es einen compiler-error geben, da es die Variable 2 mal geben w�rde (in beiden Translation Units).
  65.  
  66.     // Wenn die Variable static ist, gibt es keinen compiler-error, da die ODR unterdr�ckt wird,
  67.     // es kann bzw. muss sogar in jedem Translation Unit eine Kopie der variable geben.
  68.     // Das bedeutet wiederum, dass trotz static die Variable in verschiedenen Translation Units verschiedene Werte haben kann
  69.     // (kommt daher, dass sie auch in jedem Translation Unit eine andere Adresse hat).
  70.     // F�r static Funktionen gilt das selbe: es gibt in jedem Tranlation Unit eine kopie der Funktion.
  71.  
  72.     static int value = 10;
  73.  
  74.     // siehe beide funktionen + output
  75.     void print_tu1(void);
  76.     void print_tu2(void);
  77. }
  78.  
  79. /* FILE: test.cpp (translation unit 2) */
  80.  
  81. #include "test.h"
  82.  
  83. // siehe output von print_tu1 und print_tu2 unten (ist der Output von beiden Translation Units)
  84. void test_namespace::print_tu2(void)
  85. {
  86.     std::cout << "Translation unit 2: " << std::endl;
  87.     std::cout << "Value: " << test_namespace::value << std::endl;
  88.     std::cout << "Address of value: " << &test_namespace::value << std::endl;
  89.     std::cout << std::endl;
  90. }
  91.  
  92. /*
  93.  
  94. OUTPUT:
  95.  
  96. Call to func1 (static):
  97. 6
  98. 7
  99. 8
  100.  
  101. Call to func2 (non static):
  102. 6
  103. 6
  104. 6
  105.  
  106. Translation unit 1:
  107. Value: 75
  108. Address of value: 00007FF72F1F6000
  109.  
  110. Translation unit 2:
  111. Value: 10
  112. Address of value: 00007FF72F1F6008
  113. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement