c语言静态关键字_C ++中的静态关键字

c语言静态关键字

Static is a keyword in C++ used to give special characteristics to an element. Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime. Static Keyword can be used with following,

静态是C ++中的关键字,用于为元素赋予特殊的特性。 静态元素在程序生存期内仅在静态存储区域中分配一次存储。 而且它们的作用范围一直到程序生存期。 静态关键字可以与以下内容一起使用,

  1. Static variable in functions

    函数中的静态变量

  2. Static Class Objects

    静态类对象

  3. Static member Variable in class

    类中的静态成员变量

  4. Static Methods in class

    类中的静态方法

函数内部的静态变量 (Static Variables inside Functions)

Static variables when used inside function are initialized only once, and then they hold there value even through function calls.

静态变量在函数内部使用时仅被初始化一次,然后即使通过函数调用它们也在那里保留值。

These static variables are stored on static storage area , not in stack.

这些静态变量存储在静态存储区域中,而不是堆栈中。

void counter()
{
    static int count=0;
    cout << count++;
}

int main(0
{
    for(int i=0;i<5;i++)
    {
        counter();
    }
}

0 1 2 3 4

0 1 2 3 4

Let's se the same program's output without using static variable.

让我们使用不使用静态变量的相同程序的输出。

void counter()
{
    int count=0;
    cout << count++;
}

int main(0
{
    for(int i=0;i<5;i++)
    {
        counter();
    }
}

0 0 0 0 0

0 0 0 0 0

If we do not use static keyword, the variable count, is reinitialized everytime when counter() function is called, and gets destroyed each time when counter() functions ends. But, if we make it static, once initialized count will have a scope till the end of main() function and it will carry its value through function calls too.

如果不使用static关键字,则每次调用counter()函数时都会将变量count初始化,并在counter()函数结束时将其销毁。 但是,如果我们将其设为静态,则一旦初始化的count就会一直作用到main()函数的结尾,并且它也将通过函数调用来携带其值。

If you don't initialize a static variable, they are by default initialized to zero.

如果不初始化静态变量,则默认情况下会将它们初始化为零。

静态类对象 (Static Class Objects)

Static keyword works in the same way for class objects too. Objects declared static are allocated storage in static storage area, and have scope till the end of program.

静态关键字对于类对象也以相同的方式起作用。 声明为静态的对象被分配到静态存储区中,并且其作用域一直到程序结束。

Static objects are also initialized using constructors like other normal objects. Assignment to zero, on using static keyword is only for primitive datatypes, not for user defined datatypes.

静态对象也使用其他普通对象一样的构造函数进行初始化。 使用static关键字分配为零仅适用于原始数据类型,不适用于用户定义的数据类型。

class Abc
{
    int i;
    public:
    Abc()
    {
        i=0;
        cout << "constructor";
    }
    ~Abc()
    {
        cout << "destructor";
    }
};

void f()
{
    static Abc obj;
}

int main()
{
    int x=0;
    if(x==0)
    {
        f();
    }
    cout << "END";
}

constructor END destructor

构造函数END析构函数

You must be thinking, why was the destructor not called upon the end of the scope of if condition, where the reference of object obj should get destroyed. This is because object was static, which has scope till the program's lifetime, hence destructor for this object was called when main() function exits.

您必须要思考,为什么在if条件范围的末尾不调用析构函数,在该条件下对象obj的引用应该被销毁。 这是因为对象是static ,它的作用域一直到程序的生命周期,因此在main()函数退出时将调用此对象的析构函数。

类中的静态数据成员 (Static Data Member in Class)

Static data members of class are those members which are shared by all the objects. Static data member has a single piece of storage, and is not available as separate copy with each object, like other non-static data members.

类的静态数据成员是所有对象共享的那些成员。 静态数据成员只有一个存储空间,不能像其他非静态数据成员一样作为每个对象的单独副本使用。

Static member variables (data members) are not initialied using constructor, because these are not dependent on object initialization.

静态成员变量(数据成员)不使用构造函数初始化,因为它们不依赖于对象初始化。

Also, it must be initialized explicitly, always outside the class. If not initialized, Linker will give error.

另外,必须在类外部始终显式初始化它。 如果未初始化,链接器将给出错误。

class X
{
    public:
    static int i;
    X()
    {
        // construtor
    };
};

int X::i=1;

int main()
{
    X obj;
    cout << obj.i;   // prints value of i
}

1

1个

Once the definition for static data member is made, user cannot redefine it. Though, arithmetic operations can be performed on it.

一旦定义了static数据成员,用户便无法重新定义它。 但是,可以对其执行算术运算。

静态成员函数 (Static Member Functions)

These functions work for the class as whole rather than for a particular object of a class.

这些函数对整个类有用,而不是对类的特定对象起作用。

It can be called using an object and the direct member access . operator. But, its more typical to call a static member function by itself, using class name and scope resolution :: operator.

可以使用对象和直接成员访问来调用它. 操作员。 但是,使用类名称和作用域解析::运算符本身调用静态成员函数更为典型。

For example:

例如:

class X
{
    public:
    static void f()
    {
        // statement
    }
};

int main()
{
    X::f();   // calling member function directly with class name
}

These functions cannot access ordinary data members and member functions, but only static data members and static member functions.

这些函数不能访问普通数据成员和成员函数,而只能访问静态数据成员和静态成员函数。

It doesn't have any "this" keyword which is the reason it cannot access ordinary members. We will study about "this" keyword later.

它没有任何“ this”关键字,这是它无法访问普通成员的原因。 稍后我们将研究“ this”关键字。

翻译自: https://www.studytonight.com/cpp/static-keyword.php

c语言静态关键字

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值