C 中的静态和 C++ 中的静态的区别??

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/943280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:05:23  来源:igfitidea点击:

Difference between static in C and static in C++??

c++c

提问by sourabh jaiswal

What is the difference between the static keyword in C and C++?

C 和 C++ 中的 static 关键字有什么区别?

回答by paxdiablo

The statickeyword serves the same purposes in C and C++.

static关键字在 C 和 C++ 中具有相同的用途。

  1. When used at file level (outside of a function), it sets the visibility of the item it's applied to. Static items are not visible outside of their compilation unit (e.g., to the linker). Their duration is the same as the duration of the program.

    These file-level items (functions and data) should be static unless there's a specific need to access them from outside (and there's almost never a need to give direct access to data since that breaks the central tenet of encapsulation).

    If (as your comment to the question indicates) this is the only use of staticyou're concerned with then, no, there is no difference between C and C++.

  2. When used within a function, it sets the duration of the item. Again, the duration is the same as the program and the item continues to exist between invocations of that function.

    It does not affect the visibility of that item since it's visible only within the function. An example is a random number generator that needs to keep its seed value between invocations but doesn't want that value visible to other functions.

  3. C++ has one more use, staticwithin a class. When used there, it becomes a single class variable that's common across all objects of that class. One classic example is to store the number of objects that have been instantiated for a given class.

  1. 在文件级别(在函数之外)使用时,它会设置它所应用到的项目的可见性。静态项目在它们的编译单元之外是不可见的(例如,对于链接器)。它们的持续时间与程序的持续时间相同。

    这些文件级项目(函数和数据)应该是静态的,除非有特定的需要从外部访问它们(并且几乎从不需要直接访问数据,因为这违反了封装的中心原则)。

    如果(正如您对问题的评论所表明的那样)这是static您所关心的唯一用途,那么,不,C 和 C++ 之间没有区别。

  2. 在函数中使用时,它设置项目的持续时间。同样,持续时间与程序相同,并且该项目在该函数的调用之间继续存在。

    它不会影响该项目的可见性,因为它仅在函数内可见。一个例子是一个随机数生成器,它需要在调用之间保持其种子值,但不希望该值对其他函数可见。

  3. C++ 还有一个用途,static在一个类中。在那里使用时,它变成了一个类变量,该变量在该类的所有对象中都是通用的。一个经典示例是存储已为给定类实例化的对象数量。

As others have pointed out, the use of file-level static has been deprecated in favour of unnamed namespaces. However, I believe it'll be a cold day in a certain warm place before it's actually removed from the language - there's just too much code using it at the moment. And ISO C have only justgotten around to removing gets()despite the amount of time we've all known it was a dangerous function.

正如其他人所指出的,文件级静态的使用已被弃用,以支持未命名的命名空间。但是,我相信在它真正从语言中删除之前,它会在某个温暖的地方度过寒冷的一天——目前使用它的代码太多了。尽管我们都知道这是一个危险的功能,但ISO C 才刚刚开始删除gets()

And even though it's deprecated, that doesn't change its semantics now.

而且,即使它弃用,这并没有改变它的语义现在

回答by UncleO

The use of static at the file scope to restrict access to the current translation unit is deprecated in C++, but still acceptable in C.

在文件范围内使用 static 来限制对当前翻译单元的访问在 C++ 中已被弃用,但在 C 中仍然可以接受。

Instead, use an unnamed namespace

相反,使用未命名的命名空间

namespace
{
    int file_scope_x;
}

Variables declared this way are only available within the file, just as if they were declared static.

以这种方式声明的变量仅在文件中可用,就像它们被声明为静态一样。

The main reason for the deprecation is to remove one of the several overloaded meanings of the static keyword.

弃用的主要原因是删除了 static 关键字的几种重载含义之一。

Originally, it meant that the variable, such as in a function, would be given storage for the lifetime of the program in an area for such variables, and not stored on the stack as is usual for function local variables.

最初,这意味着变量(例如在函数中)将在程序的生命周期内存储在此类变量的区域中,而不是像函数局部变量通常那样存储在堆栈中。

Then the keyword was overloaded to apply to file scope linkage. It's not desirable to make up new keywords as needed, because they might break existing code. So this one was used again with a different meaning without causing conflicts, because a variable declared as static can't be both inside a function and at the top level, and functions didn't have the modifier before. (The storage connotation is totally lost when referring to functions, as they are not stored anywhere.)

然后关键字被重载以应用于文件范围链接。根据需要组成新关键字是不可取的,因为它们可能会破坏现有代码。所以这个又用了不同的意思,又不会引起冲突,因为声明为static的变量不能既在函数内部又在顶层,而且函数之前没有修饰符。(在提到函数时,存储的含义完全丢失了,因为它们没有存储在任何地方。)

When classes came along in C++ (and in Java and C#) the keyword was used yet again, but the meaning is at least closer to the original intention. Variables declared this way are stored in a global area, as opposed to on the stack as for function variables, or on the heap as for object members. Because variables cannot be both at the top level and inside a class definition, extra meaning can be unambiguously attached to class variables. They can only be referenced via the class name or from within an object of that class.

当类出现在 C++(以及 Java 和 C#)中时,关键字再次被使用,但其含义至少更接近最初的意图。以这种方式声明的变量存储在全局区域中,而不是像函数变量那样存储在堆栈中,或者像对象成员那样存储在堆中。因为变量不能既位于顶层又位于类定义内,所以可以明确地将额外的含义附加到类变量上。它们只能通过类名或从该类的对象中引用。

回答by Martin York

It has the same meaning in both languages.

它在两种语言中具有相同的含义。

But C++ adds classes. In the context of a class (and thus a struct) it has the extra meaning of making the method/variable class members rather members of the object.

但是 C++ 添加了类。在类(以及结构)的上下文中,它具有使方法/变量类成员而不是对象成员的额外含义。

class Plop
{
    static int x; // This is a member of the class not an instance.

    public:
    static int getX() // method is a member of the class.
    {
        return x;
    }
};

int Plop::x  = 5;

回答by Martin York

Note that the use of static to mean "file scope" (aka namespace scope) is only deoprecated by the C++ Standard for objects, not for functions. In other words,:

请注意,使用 static 来表示“文件范围”(又名命名空间范围)仅被 C++ 标准弃用用于对象,而不是用于函数。换句话说,:

// foo.cpp  
static int x = 0;               // deprecated
static int f() { return 1; }    // not deprecated

To quote Annex D of the Standard:

引用标准的附录 D:

The use of the static keyword is deprecated when declaring objects in namespace scope.

在命名空间范围内声明对象时,不推荐使用 static 关键字。

回答by Tobin

You can not declare a static variable inside structure in C... But allowed in Cpp with the help of scope resolution operator.

您不能在 C 中的结构内部声明静态变量......但在 Cpp 中允许在范围解析运算符的帮助下。

Also in Cpp static function can access only static variables but in C static function can have static and non static variables...

同样在 Cpp 静态函数中只能访问静态变量,但在 C 静态函数中可以有静态和非静态变量......