Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Light-City committed Apr 10, 2022
1 parent 02d6ffa commit 2e087d1
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions basic_content/static/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,32 @@ int main()
Welcome to Apple!
```

**限定访问范围**
static还有限定访问范围的作用(类似于匿名名字空间)。来自issue:https://github.com/Light-City/CPlusPlusThings/issues/142

// source1.cpp
extern void sayHello();
const char* msg = "Hello World!\n";
int main()
{
sayHello();
return 0;
}

// source2.cpp
#include <cstdio>
extern char* msg;
void sayHello()
{
printf("%s", msg);
}
g++对于上面两个代码文件是可以正常编译并且打印Hello World!,但如果给source1.cpp中的msg加上static,则会导致undefined reference to 'msg'的编译错误:

// source1.cpp
extern void sayHello();
static const char* msg = "Hello World!\n";
int main()
{
sayHello();
return 0;
}

0 comments on commit 2e087d1

Please sign in to comment.