本文介绍了什么是用C或C ++中的静态块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是静态块C或C ++用一个例子?我知道什么是静态的,而是什么是静态和静态块之间的区别?

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?

推荐答案

另一种方法是,你可能会寻找一个静态的比喻
阻止Java的。应用程序加载时运行code块。
在C中没有这样的事情++,但它可以通过使用一个构造函数被伪造
静态对象。

Another alternative is that you might be looking for the analogy of a staticblock 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 astatic object.

foo.cpp:

struct StaticBlock {
    StaticBlock(){
        cout << "hello" << endl;
    }
}


static StaticBlock staticBlock;

void main(int, char * args[]){

}

但是。我通过这之前,因为它是℃的微妙的边缘的情况下被咬伤++
标准。如果静态对象不是由主被叫任何code可达
静态对象的构造可以或可以不被调用。

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 theconstructor of the static object may or may not be called.

我发现用gcc你好将获得输出,并与Visual Studio它会
没有。

I found that with gcc hello will get output and with visual studio it willnot.

这篇关于什么是用C或C ++中的静态块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:36