Cpp -- static变量不属于类的实例

 

在Cpp中,类中的Static变量不属于任何一个实例。

下面,使用两种方法具体证明。

1、

class Student{
private:
    string name;
    int age;
    int height;
    static int money;

public:
    Student(string inputName,int inputAge):name(inputName),age(inputAge){
    
    }
    void displayStudent(){
        cout<<name<<endl;
        cout<<age<<endl;
    }
};

 

int main()
{
    fstream fs("out.dat",ios::out | ios::binary);

    Student stu1("he",14);
    
    fs.write(reinterpret_cast<char *>(&stu1),sizeof(stu1));

    fs.close();

    system("pause");
    return 0;
}

查看该文件,可以发现,并没有Static变量money的踪影。

 

2、使用指令查看内存

从内存中可以看出,类中依次有 name,age,height,没有Static变量money的踪影。

posted on 2015-07-24 20:20  依风152  阅读(400)  评论(0编辑  收藏  举报