有如下程序: #include using namespace std; class Test{ public: Test() {n+=2;} ~Test(){n-=3;) static im ge

admin2012-12-29  14

问题 有如下程序:
    #include
    using namespace std;
    class Test{
    public:
    Test()  {n+=2;}
    ~Test(){n-=3;)
    static im getNum(){return n;}
    private:
    staticintn;
    };
    int Test:n=1;
    int main()
    {
    Test*p=new Test;
    delete p;
    cout<<“n;”<    return 0;
    }
    执行后的输出结果是(             )。

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

答案A

解析 语句Test*p=new Test;会调用类的构造函数’rest(){n+=2;},使n的值由原来的1变为3,然后deletep调用类的析构函数~Test(){n-=3.},因为n是static型变量,所以会在3的基础上减3使得输出结果为0。
转载请注明原文地址:https://kaotiyun.com/show/s0Vp777K
0

随机试题
最新回复(0)