简体   繁体   English

静态成员与静态全局

[英]Static member vs static global

I have read that the difference between globals and static globals is that the global variable can be referred to in another implementation file via extern, whereas static globals are localized to only that implementation file. 我已经读过全局变量和静态全局变量之间的区别在于全局变量可以通过extern在另一个实现文件中引用,而静态全局变量仅局限于该实现文件。 See these two questions for more information: [ 1 , 2 ]. 看到这两个问题,以了解更多信息:[ 12 ]。

From what I understand, this means the that the following foo() and bar() should be linked identically. 据我所知,这意味着以下foo()bar()应该相同地链接。 Both functions can only be used by MyClass . 这两个函数只能由MyClass

//MyClass.h
Class MyClass{
private:
  static void foo();
};

//MyClass.cpp
void MyClass::foo(){}
static void bar(){}

I can see foo() 's declaration being more common since it lets the header file lay out the entire class more completely (even if you can't/shouldn't use the private stuff), but is bad practice declare a function like bar() (hidden from the header file)? 我可以看到foo()的声明更常见,因为它让头文件更完整地布局整个类(即使你不能/不应该使用私有的东西),但是不好的做法声明一个类似的函数bar() (从头文件中隐藏)?

For context, I am defining a WNDPROC for windows messages which needs to be static to work, but it's a rather ugly declaration and I'm not sure if I should hide it completely in the implementation file or go ahead and declare it in the header file. 对于上下文,我正在为Windows消息定义一个WNDPROC ,它需要静态才能工作,但这是一个相当丑陋的声明,我不确定是否应该将它完全隐藏在实现文件中,或者继续在头文件中声明它文件。

static is a very horrible keyword as it has many different meanings depending on the context. static是一个非常可怕的关键字,因为它具有许多不同的含义,具体取决于上下文。 static variables and static functions are completely different, and a static function in a class and a static free-function are completely different. 静态变量和静态函数完全不同,类中的静态函数和静态自由函数完全不同。

A static function in a class means that the function can be called without an instance of the class, but it cannot access non-static members of the class. 类中的静态函数意味着可以在没有类实例的情况下调用函数,但它不能访问类的非静态成员。 It is a bit like a regular function, just enclosed in the class for tidiness purposes. 它有点像常规函数,只是为了整洁目的而封装在类中。

A static free-function has internal linkage, so it cannot be seen outside of the source file and its name can be reused in other source files. 静态自由函数具有内部链接,因此在源文件之外无法看到它,并且其名称可以在其他源文件中重用。

A static class function does not have internal linkage. 静态类函数没有内部链接。 All class functions have external linkage. 所有类功能都有外部链接。 You can split the class function between header and source files whether the class function is static or not. 无论类函数是否为静态,您都可以在头文件和源文件之间拆分类函数。

I recommend you read some tutorials/books to understand the many different uses of static more clearly. 我建议你阅读一些教程/书籍,以便更清楚地了解静态的许多不同用法。 When you see static in a place you've not seen it before, assume nothing! 当你在一个你以前没见过的地方看到静电时,什么都不做!

If you have a free-function which you want hide in a source file, you can declare it static as you have done so. 如果您有一个要隐藏在源文件中的自由函数,则可以将其声明为静态函数。 Alternatively you can place it in an unnamed namespace. 或者,您可以将其放在未命名的命名空间中。

// cpp file only
namespace
{
    void hiddenfunc() {..}
}

This is similar to 这类似于

static void hiddenfunc();

And it can be called in the same way (just as "hiddenfunc()"). 它可以以相同的方式调用(就像“hiddenfunc()”)。 An advantage of unnamed namespaces (a weird name, I know) is that you can also place classes and other definitions, that you only want to be visible within that source file. 未命名的命名空间(我知道一个奇怪的名称)的一个优点是,您还可以放置您希望在该源文件中可见的类和其他定义。 Just make sure you define the function body within the namespace {..} area. 只需确保在命名空间{..}区域中定义函数体。 Don't put an unnamed namespace in a header file. 不要在头文件中放入未命名的命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM