1. 下面的代码输出什么?为什么?
#include <iostream>
#include <malloc.h>
using namespace std;
class A
{
private:
static int c_count;
public:
A()
{
c_count++;
}
~A()
{
c_count--;
}
static void Count()
{
cout<<c_count<<endl;
}
};
int A::c_count = 0;
int main()
{
A* a = static_cast<A*>(malloc(sizeof(A))); // malloc()函数,不会触发构造函数,不会初始化为0
a->Count(); // 0
delete a; // -1 关键字new、delete触发构造函数、析构函数
a->Count(); // 静态成员函数的调用,通过类名 A::Count() ,不是使用野指针
return 0;
}
笔试题
最新推荐文章于 2024-07-25 15:12:21 发布