c ++静态对象和类的静态成员 [英] c++ static object and static member of class

查看:81
本文介绍了c ++静态对象和类的静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;
class A{
public:
    static int x;
    static A y;
    A();
    ~A();
};
A::A() {
    y.x++;
}
A::~A()
{
    y.x--;
}
A A::y=*new A;
int A::x=5;
void main(){
    A a;
    A b;
    cout<<a.x<<endl;
    cout<<b.x<<endl;
}



你好家伙

i camt明白这段代码....这是什么意思(


hello guys
i camt understand this code .... what does it mean to do this (

A A::y=*new A;
int A::x=5;



)以及将类的静态对象放在一边是什么意思......

甚至看完变量之后...我无法理解这个

任何帮助请...:''(


) and what''s the point of having a static object of the class in side it ....
even after watching the variables ... i couldnt understand this
any help please .. :''(

推荐答案

除了回答1 2,重要的是要注意,因为 A 构造函数增量 x 静态成员,所以要注意这一点很重要在动态初始化之前初始化常量表达式。



因此 a 初始化为 5 首先。

然后初始化 y 将导致 x 增加到 6



然后在main中,对象 a b 将导致 x 增加两次。因此,在调用两个构造函数后,该值将为8.



因为 x 是静态的 cout 语句将输出相同的变量,该变量将保存 8 。因此输出将是:



In addition to answer 1 and 2, it is important to notice that since A constructor increment x static member, it is important to be aware that constant expression are initialized before dynamic initialization.

Thus a is initialized to 5 first.
Then the initialization of y will cause x to increment to 6.

Then in main, objects a and b will cause x to be incremented twice. Thus the value would be 8 after both constructors are called.

Since x is static both cout statements will output the same variable which will hold 8 at that point. Thus the output will be:

8
8





最后有趣的是,由于未定义复制构造函数,如果复制构造函数已被使用,计数将无法平衡。



此外,由于有一个新的用于初始化静态变量,因此在程序结束时不会调用相应的析构函数。



当退出main时,将调用 b a 的析构函数,因此<$ c当程序即将退出时,$ c> x 的值为 6



在这种情况下它并不重要,但如果计数器已用于必须明确发布的资源(通常是进程外资源),那么它就会出现问题。



Finally it is interesting to note that since the copy constructor is not defined, counting would not balance if the copy constructor would have been used.

Also since there is a new used to initialize a static variable there will be no corresponding destructor called at the end of the programm.

When main is exited, destructors for b and a are called thus x will have a value of 6 when the program is about to exit.

In this case it does not matter much but if the counter would have been used for ressources that must be explicitly released (typically out-of-process ressources), then it would have been problematic.


A A::y=*new A;
int A::x=5;



这些语句初始化静态变量: x 初始化为 5 y 初始化为 A 对象的新实例。您应该能够通过单步执行代码来了解其他变量的初始化和更改方式。



您还应该建议您获得一本好书或者关于C ++的教程。另请参见 Bjarne Stroustrup'的主页 [ ^ ]





正如Philippe在下面指出的,初始化 static 对象在执行任何其他代码之前发生。

[/ edit]


These statements initialise the static variables: x is intialised to 5, and y is initialised to a new instance of an A object. You should be able to see how the other variables are initialised and changed by stepping through the code.

You would also be well advised to get hold of a good book or tutorial on C++. See also Bjarne Stroustrup''s homepage[^]

[edit]
As Philippe points out below, the initialisation of static objects takes place before any other code is executed.
[/edit]


A A::y=*new A;



包含类型为 A 的静态字段,名称为 y 。此语句创建 A 的新实例,并将其分配给类本身的静态字段。


The A class contain a static field of type A, with name y. This statement creates a new instance of A and assigns it to the static field of the class itself.

int A::x=5;



x 也是 A ,它被赋值 5 。这用作实例计数器。如果查看代码,您将看到,每次创建实例时,计数器都会递增,并在销毁时递减。内部实例仅用于访问静态计数器,但这通常是无用的。

如我所见,这是一个需要考虑的代码,没有实际用途。所以为什么的答案实际上是为什么不:)。


x is also a static member of A, it is assigned the value 5. This is used as an instance counter. If you look at the code, you will see, that every time an instance is created, the counter is incremented, and decremented on destruction. The inner instance is only used to access the static counter, but this is useless in general.
As I see, this is a code to think about, with no actual use. So the answer to "why" is actually "why not" :).


这篇关于c ++静态对象和类的静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆