#include <iostream> using namespace std; class A { static int obj_count; public: A() { obj_count++; } ~A() { obj_count--; } int get_num_of_objects() { return obj_count; } }; int A::obj_count = 0; A a; int main() { A b, *p, *q; p = new A; q = new A[5]; cout << a. get_num_of_objects() << '\t'; delete []q; cout << p->get_num_of_objects() << '\t'; for(int i = 0; i < 2; i++) { A c; cout << c.get_num_of_objects() << '\t'; } delete p; cout << b.get_num_of_objects() << endl; return 0; }
时间: 2024-03-27 12:41:51 浏览: 13
这段代码的作用是统计类A的对象数量。类A中有一个静态成员变量obj_count,用于记录A类的对象个数。每次创建A类的对象时,obj_count自增1;每次销毁A类的对象时,obj_count自减1。在main函数中,首先创建了一个全局的A类对象a,此时obj_count为1;接着创建了三个局部的A类对象b、c和一个动态分配的A对象p,此时obj_count为5;再动态分配了5个A类对象q,此时obj_count为10。接着输出a.get_num_of_objects()的结果,即1;删除q数组后输出p->get_num_of_objects()的结果,即6;之后又创建了两个局部的A类对象c,输出它们的get_num_of_objects()的结果,即7、8;最后删除p和b对象,输出b.get_num_of_objects()的结果,即6。
相关问题
c++中static关键字的用法
在C++中,static关键字有多种用法,下面是两个常见的用法示例:
1. 在函数内部使用static修饰局部变量:
```cpp
#include <iostream>
using namespace std;
void increment() {
static int count = 0; // 使用static修饰局部变量
count++;
cout << "Count: " << count << endl;
}
int main() {
increment(); // 输出:Count: 1
increment(); // 输出:Count: 2
increment(); // 输出:Count: 3
return 0;
}
```
在上面的示例中,使用static修饰的局部变量count在函数调用之间保持其值,而不会被重置为初始值。
2. 在类中使用static修饰成员变量:
```cpp
#include <iostream>
using namespace std;
class MyClass {
public:
static int count; // 使用static修饰成员变量
};
int MyClass::count = 0; // 静态成员变量需要在类外进行初始化
int main() {
MyClass obj1;
MyClass obj2;
obj1.count = 5;
cout << "obj1.count: " << obj1.count << endl; // 输出:obj1.count: 5
cout << "obj2.count: " << obj2.count << endl; // 输出:obj2.count: 5
obj2.count = 10;
cout << "obj1.count: " << obj1.count << endl; // 输出:obj1.count: 10
cout << "obj2.count: " << obj2.count << endl; // 输出:obj2.count: 10
return 0;
}
```
在上面的示例中,使用static修饰的成员变量count是类的所有对象共享的,它的值在所有对象之间保持一致。
用具体代码演示static关键字
好的,下面是一个示例:
```
#include <iostream>
using namespace std;
class MyClass {
public:
static int count; // 静态成员变量
MyClass() {
count++;
}
static void showCount() { // 静态成员函数
cout << "Object count: " << count << endl;
}
};
int MyClass::count = 0; // 静态成员变量初始化
int main() {
MyClass obj1;
MyClass obj2;
MyClass::showCount();
return 0;
}
```
在上面的代码中,我们定义了一个`MyClass`类,包含一个静态成员变量`count`和一个静态成员函数`showCount`。在`main`函数中,我们创建了两个`MyClass`对象,调用了静态成员函数`showCount`,输出了对象的数量。
需要注意的是,静态成员变量必须在类外进行初始化,这里我们通过`MyClass::count = 0`来初始化。而静态成员函数只能访问静态成员变量,不能访问非静态成员变量。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)