成员变量和成员函数占用内存空间

#include <iostream>

//每个空对象也分配1个字节空间,区分空对象内存位置。每个空对象也有一个独一无二的内存地址
class Person
{
};

class Student
{
   //静态成员不属于类对象
   static int voice;
   static void func();
};

class Worker
{
   int age; //非静态成员属于类对象
};

int main()
{
   std::cout << "Person size " << sizeof(Person) << std::endl;
   std::cout << "Student size " << sizeof(Student) << std::endl;
   std::cout << "Worker size " << sizeof(Worker) << std::endl;

   return 0;
}
$ ./a.out          
Person size 1
Student size 1
Worker size 4
posted @ 2022-05-30 16:39  thomas_blog  阅读(66)  评论(0编辑  收藏  举报