#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
int m_A; //非静态成员变量,属于对象身上
void func() {}; //非静态成员函数 不属于对象身上
static int m_B; //静态成员变量 ,不属于对象身上
static void func2() {}; //静态成员函数 ,不属于对象身上
double m_C; // 12——错误 16——正确; 原因:内存对齐!
};
//结论 : 非静态成员变量 ,才属于对象身上
void test01()
{
cout << "size of (Person) = " << sizeof(Person) << endl; //输出值为:16
int a = 10; //求int型的size时,要记得赋值!
cout << "size of int = " << sizeof(a) << endl; //输出值为:4
double b = 20.0;
cout << "size of double = " << sizeof(b) << endl; //输出值为:8
//空类的大小为:1 每个实例的对象 都有独一无二的地址,char维护这个地址
}
void test02()
{
//this指针指向被调用的成员函数所属的对象
Person p1;
p1.func(); //编译器会偷偷 加入一个 this指针 Person* this
Person p2;
p2.func();
}
int main(){
test01();
system("pause");
return 0;
}
关于成员变量和成员函数分开存储!
最新推荐文章于 2024-07-14 16:08:44 发布