Created
June 22, 2016 09:34
-
-
Save klmr/9e06284a1ca9152bee2bfc3b1df14003 to your computer and use it in GitHub Desktop.
Difference between static and inline functions in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| inline int f() { | |
| static int x = 0; | |
| return ++x; | |
| } | |
| static int g() { | |
| static int x = 0; | |
| return ++x; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "header.hpp" | |
| #include <iostream> | |
| void test(); | |
| int main() { | |
| std::cout << "From main:\n"; | |
| std::cout << "f() = " << f() << ", g() = " << g() << '\n'; | |
| std::cout << "From test:\n"; | |
| test(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "header.hpp" | |
| #include <iostream> | |
| void test() { | |
| std::cout << "f() = " << f() << ", g() = " << g() << '\n'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: