I want to know that what is static block in c or c++ with an example? I know what is static but what is the difference between static and static block?
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
In C++ there is the concept of an anonymous namespace.
to get the same effect in C
In simple terms the compiler does not export symbols from translation units when they are either declared static or in an anonymous namespace.
There is no concept with the name "static block" in C/C++. Java has it however, a "static block" is an initializer code block for a class which runs exactly once, before the first instance of a class is created. The basic concept 'stuff that runs exactly once' can simulated in C/C++ with a static variable, for example:
This is not thread-safe however. Getting this working right in the presence of multiple threads can be difficult and tricky sometimes.
I found this answer on The Code Project. It involves having an extra static variable, but I believe it is more reliable than bradgonesurfing's answer. Basically, it is this:
It also means that, like Java's static blocks, you are not required to ever actually have an instance of
class Foo
, which is useful when the class can take a lot of data, and you simply need to automagically call something before it loads, not instantiate an extra instance of it. You can test that exact code block. I just compiled it (with a little output from static_init(), and had main() print Foo::__st_init, just to make sure), and it worked just fine.EDIT:
Sorry that this is so late, but I tested what bradgonesurfing mentioned:
I used the following inside main.cpp:
I compiled with
g++ ./main.cpp -o main
and ran it and recieved a friendly "Hello, World!" message on my console. Just to be thorough, I also compiled the same version but without the printing and compiled withg++ ./main.cpp -g -o main
. I then ran the executable with gdb and had the following result:Here's a more current version output for g++:
g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Another alternative is that you might be looking for the analogy of a static block in Java. A block of code that is run when the application is loaded. There is no such thing in C++ but it can be faked by using the constructor of a static object.
HOWEVER. I've been bitten by this before as it's a subtle edge case of the C++ standard. If the static object is not reachable by any code called by main the constructor of the static object may or may not be called.
I found that with gcc hello will get output and with visual studio it will not.
While indeed, C++ does not have static blocks as part of the language, you can implement static blocks without you (as a user) having to use any classes or namespaces, and can write:
or whatever else you want. You can't have those within classes, though, just at file scope. See a detailed description of these in my answer to a related question, and the code for
static_block.h
here.Note: This does not require C++11 and will work well with old compilers.