zoukankan      html  css  js  c++  java
  • 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的踪影。

  • 相关阅读:
    2.25家庭记账本小软件
    2.10简单体温记录小软件总结
    4.26PHP
    4.25Android
    4.24css
    4.23css
    4.22电梯演讲
    4.21python
    4.20python
    4.19python
  • 原文地址:https://www.cnblogs.com/wuqi/p/4674445.html
Copyright © 2011-2022 走看看