Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* FILE: main.cpp (translation unit 1) */
- #include <iostream>
- #include "test.h"
- void func1(void)
- {
- // static Variablen werden einmal beim Programmstart angelegt (und initialisiert),
- // das hei�t der Wert bleibt au�erhalb vom Scope erhalten (siehe output unten).
- static int var = 5;
- std::cout << ++var << std::endl;
- }
- void func2(void)
- {
- // Hier wird jedes mal wenn das Programm diese Funktion ausf�hrt die Variable neu angelegt und initalisiert.
- // Der Wert bleibt au�erhalb vom Scope NICHT erhalten (siehe output unten).
- int var = 5;
- std::cout << ++var << std::endl;
- }
- // zuerst test.h lesen (weiter unten)
- void test_namespace::print_tu1(void)
- {
- std::cout << "Translation unit 1: " << std::endl;
- test_namespace::value = 75; // Wert wird nur in diesem Tranlation unit ver�ndert.
- std::cout << "Value: " << test_namespace::value << std::endl;
- std::cout << "Address of value: " << &test_namespace::value << std::endl;
- std::cout << std::endl;
- }
- int main()
- {
- std::cout << "Call to func1 (static): " << std::endl;
- func1();
- func1();
- func1();
- std::cout << std::endl;
- std::cout << "Call to func2 (non static): " << std::endl;
- func2();
- func2();
- func2();
- std::cout << std::endl;
- test_namespace::print_tu1();
- test_namespace::print_tu2();
- return 0;
- }
- /* FILE: test.h */
- #pragma once
- #include <iostream>
- namespace test_namespace
- {
- // static kann auch noch eine andere Bedeutung haben, n�mlich unterdr�ckt es die ODR (One Definition Rule).
- // W�rde ich jetzt eine variable
- // int value = 10;
- // global anlegen, wird es einen compiler-error geben, da es die Variable 2 mal geben w�rde (in beiden Translation Units).
- // Wenn die Variable static ist, gibt es keinen compiler-error, da die ODR unterdr�ckt wird,
- // es kann bzw. muss sogar in jedem Translation Unit eine Kopie der variable geben.
- // Das bedeutet wiederum, dass trotz static die Variable in verschiedenen Translation Units verschiedene Werte haben kann
- // (kommt daher, dass sie auch in jedem Translation Unit eine andere Adresse hat).
- // F�r static Funktionen gilt das selbe: es gibt in jedem Tranlation Unit eine kopie der Funktion.
- static int value = 10;
- // siehe beide funktionen + output
- void print_tu1(void);
- void print_tu2(void);
- }
- /* FILE: test.cpp (translation unit 2) */
- #include "test.h"
- // siehe output von print_tu1 und print_tu2 unten (ist der Output von beiden Translation Units)
- void test_namespace::print_tu2(void)
- {
- std::cout << "Translation unit 2: " << std::endl;
- std::cout << "Value: " << test_namespace::value << std::endl;
- std::cout << "Address of value: " << &test_namespace::value << std::endl;
- std::cout << std::endl;
- }
- /*
- OUTPUT:
- Call to func1 (static):
- 6
- 7
- 8
- Call to func2 (non static):
- 6
- 6
- 6
- Translation unit 1:
- Value: 75
- Address of value: 00007FF72F1F6000
- Translation unit 2:
- Value: 10
- Address of value: 00007FF72F1F6008
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement