有如下程序: #include<iostream> using namespace std; Class Test { public: Test() {n+=2;} ~Test() {n-=3;} sta

admin2010-12-16  31

问题 有如下程序:    #include<iostream>    using namespace std;    Class Test    {    public:    Test()  {n+=2;}    ~Test()  {n-=3;}    static int getNum()    {return n;}    private:    static int n;    };    int Test::n=1;    int main()    {    Test*p=new Test;   

选项 A、n=0     
B、n=1
C、n=2     
D、n=3

答案A

解析  静态数据成员是类中所有对象共享的成员,而不是某个对象的成员。题中变量n是静态数据成员,对象对其操作的结果具有叠加作用,main函数中先定义了Test的对象*p,然后又delete p,所以对静态数据n进行了两次操作,分别是”n+=2”和”n+=3”,n的初始值是1,那么n最后的值变为0。main函数最后通过调用静态函数getNum得到n的值,并输出。
转载请注明原文地址:https://kaotiyun.com/show/v6Vp777K
0

最新回复(0)