一、对象模型
(一)空类占据一个字节
在C++中空类会占一个字节,这是为了让对象的实例能够相互区别。具体来说,空类同样可以被实例化,并且每个实例在内存中都有独一无二的地址,因此,编译器会给空类隐含加上一个字节,这样空类实例化之后就会拥有独一无二的内存地址。
#include<iostream>
using namespace std;
class MyClass {
public:
};
void test01() {
MyClass mclass;
cout << sizeof(mclass) << endl;
}
int main(int argc, char const **argv) {
test01();
return 0;
}
输出结果为:1 验证空类所占空间为1字节。
(二)储存空间分布模型
类中成员函数和成员变量是分开储存的,成员函数不占用对象空间,静态成员变量不占用对象空间,成员变量占用对象空间。例如:
#include<iostream>
using namespace std;
class MyClass {
public:
MyClass() {
a = 0;
}
int a;
static int b;
void print() {
cout << "a=" << a << ",b=" << b << endl;
}
};
int MyClass::b = 10;
void test01() {
MyClass mclass;
cout << sizeof(mclass) << endl;
mclass.print();
}
int main(int argc, char const **argv) {
test01();
return 0;
}
- 4
- a=0,b=10
- 请按任意键继续. . .
案例中MyClass类中包含一个整形变量a,一个静态整型变量b,一个构造函数和一个成员函数。所占空间是4字节。
总结:当对象是空类对象时,所占空间为1字节,为了区分每个实例化空类对象;当对象非空类时,类的大小是类内非静态成员变量所占大小的和,静态成员变量不占对象内空间(储存在全局区),成员函数不占对象内空间;所有对象共用类中成员函数。
二、this指针
每一个非静态成员函数只会产生一份函数实例,也就是说多个同类型的对象会共用一块代码;
那么问题是:这一块代码是如何区分那个对象调用自己的呢?
c++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向当前实例化对象。
this指针隐含在每个非静态成员函数中,指向当前实例化对象,不需要定义,可直接使用。类中一般省略。
this指针的用途:
-
当形参和成员变量同名时,可用this指针来区分
-
在类的非静态成员函数中返回对象本身,可使用return *this
Tips:this指针只能在非静态成员函数中使用。
#include<iostream>
using namespace std;
class MyClass {
public:
MyClass() {
a = 0;
}
int a;
MyClass &func(int a) { //用于区分和成员变量同名的参数
this->a = a;
return *this; //返回对象本身
}
};
void test01() {
MyClass mclass;
cout << mclass.func(10).a << endl;
}
int main(int argc, char const **argv) {
test01();
system("pause");
return 0;
}
三、空指针访问类成员函数
当成员函数中未用到this指针时,空指针可调用该成员函数。所以,当使用空指针时,需要加上对this指针的判定,增加程序健壮性。
#include<iostream>
using namespace std;
#include<fstream>
#include<string>
class Person {
public:
Person() {
cout << "构造函数调用" << endl;
}
string m_Name;
int m_Age;
void show() {
cout << "This is Person Class." << endl;
}
void showAge() { //加入this指针的判定增加程序健壮性
if (this) {
cout << m_Age << endl;
}
}
};
void test01() {
Person *p = NULL;
p->show();
p->showAge();
}
int main(int argc, char const **argv) {
test01();
return 0;
}