对于免费(不在类内)函数:
static
意味着inline
?inline
意味着static
?
还是两者兼而有之?
考虑例子:
static void foo1() { ... };
static inline void foo2 { ... };
inline void foo3() { ... };
有什么区别?
最佳答案
有差异,考虑(在header中):
static int foo1() { static int i = 0; return ++i; }
static inline int foo2() { static int i = 0; return ++i; }
inline int foo3() { static int i = 0; return ++i; }
并在 2 cpp 中“void caller[1-2]_foo[1-3]() { std::cout << foo[1-3]() << std::endl; }
”。
所以
int main()
{
caller1_foo1(); // 1
caller2_foo1(); // 1
caller1_foo2(); // 1
caller2_foo2(); // 1
caller1_foo3(); // 1
caller2_foo3(); // 2
}
我找不到 static
之间的差异和 static inline
虽然。
关于c++ - 静态意味着内联或反之亦然或两者兼而有之?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48203341/