memset清空类的时候的3个不同效果

来源:互联网 发布:qq业务在线下单源码 编辑:程序博客网 时间:2024/06/06 00:45


#include<iostream>


using namespace std;


class A
{
        public:
        virtual void f() 
        {   
                printf("A.f called.\n");
        }   
};




class X:public A
{
        public:
                static const  int  b = 10 ;
                int cvalue()
                {   
                        return c;
                }   
        private:
                int c ; 


        public:
                X() 
                {   
                        c =20;
                }    


                 void f() 
                {   
                 cout<<"f called."<<endl;
                }   
};
int main()


{


        X x;
        cout<<x.b<<endl;
        cout<<x.cvalue()<<endl;
        x.f();
        cout<<"----------------------------------"<<endl;
        memset(&x, 0x0, sizeof(X));
        cout<<x.b<<endl;
        cout<<x.cvalue()<<endl;
        x.f();


        //类的私有成员数据会被清零
        cout<<x.cvalue()<<endl;
        x.f();




        cout<<"_______________________"<<endl;
        A* a = new X;
        a->f();
        memset(a, 0x0, sizeof(X));


        //vptr is null.
        a->f();


        delete a;




        return 0;