- 我希望你没有刻意为追求一个数字而生活,我希望你找到了真正的价值所在
- 你一定要坚持做自己,静下心来做自己喜欢的事,然后把自己交给命运
C++对象模型和this指针
成员变量和成员函数分开存储
只有非静态成员变量才属于类的对象上
class Person {
public:
Person() {
mA = 0;
}
int mA;
static int mB;
void func() {
cout << "mA:" << this->mA << endl;
}
static void sfunc() {
}
};
int main() {
cout << sizeof(Person) << endl;
system("pause");
return 0;
}
注意:
空对象占用内存空间为:1 : 原因在于 C++编译器会给每个空对象也分配一个字节空间,是为了区分空对象占内存的位置,每个空对象也应该有一个独一无二的内存地址- 非静态成员变量,属于类的对象上。静态成员变量、非静态成员函数、静态成员函数、不属于类的对象上
this指针概念
那么问题是:这一块代码是如何区分那个对象调用自己的呢?
c++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象
this指针是隐含每一个非静态成员函数内的一种指针
this指针不需要定义,直接使用即可
this指针的用途:
- 当形参和成员变量同名(
m_Age 其中m代表member成员变量)时,可用this指针来区分 - 在类的非静态成员函数中返回对象本身,可使用
return *this
class Person
{
public:
Person(int age)
{
this->age = age;
}
Person& PersonAddPerson(Person p)
{
this->age += p.age;
return *this;
}
int age;
};
void test01()
{
Person p1(10);
cout << "p1.age = " << p1.age << endl;
Person p2(10);
p2.PersonAddPerson(p1).PersonAddPerson(p1).PersonAddPerson(p1);
cout << "p2.age = " << p2.age << endl;
}
int main() {
test01();
system("pause");
return 0;
}
注意:
Person& PersonAddPerson(Person p)
{
this->age += p.age;
return *this;
}
这里使用引用的方式(Person & )返回,如果用值的方式(Person )返回,则会构造一个新的对象,根据拷贝函数
空指针访问成员函数
C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针
-
如果用到this指针,需要加以判断保证代码的健壮性 if (this == NULL) { return; } -
再者深究原因,其实cout << mAge << endl; 的mAge 为this->mAge ,空指针调用肯定报错 示例:
class Person {
public:
void ShowClassName() {
cout << "我是Person类!" << endl;
}
void ShowPerson() {
if (this == NULL) {
return;
}
cout << mAge << endl;
}
public:
int mAge;
};
void test01()
{
Person * p = NULL;
p->ShowClassName();
p->ShowPerson();
}
int main() {
test01();
system("pause");
return 0;
}
const修饰成员函数
常函数:
- 成员函数后加const后我们称为这个函数为常函数
- 常函数内不可以修改成员属性
- 成员属性声明时加关键字mutable后,在常函数中依然可以修改
常对象:
- 声明对象前加const称该对象为常对象
- 常对象只能调用常函数
示例:
class Person {
public:
Person() {
m_A = 0;
m_B = 0;
}
void ShowPerson() const {
this->m_B = 100;
}
void MyFunc() const {
}
public:
int m_A;
mutable int m_B;
};
void test01() {
const Person person;
cout << person.m_A << endl;
person.m_B = 100;
person.MyFunc();
}
int main() {
test01();
system("pause");
return 0;
}
注意:
-
this指针的本质是指针常量,指针的指向不可以更改 Person * const this -
在成员函数后面加 const,修饰的是this指向如 void showPerson() const {} ,让指针指向的值也不可以修改 其本质为 const Person * const this -
特殊变量,即使在常函数中,也可以修改这个值,加上关键字 mutable
友元
友元的关键字为 friend
友元的三种实现
全局函数做友元
class Building
{
friend void goodGay(Building * building);
public:
Building()
{
this->m_SittingRoom = "客厅";
this->m_BedRoom = "卧室";
}
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
void goodGay(Building * building)
{
cout << "好基友正在访问: " << building->m_SittingRoom << endl;
cout << "好基友正在访问: " << building->m_BedRoom << endl;
}
void test01()
{
Building b;
goodGay(&b);
}
int main(){
test01();
system("pause");
return 0;
}
类做友元
class Building;
class goodGay
{
public:
goodGay();
void visit();
private:
Building *building;
};
class Building
{
friend class goodGay;
public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
Building::Building()
{
this->m_SittingRoom = "客厅";
this->m_BedRoom = "卧室";
}
goodGay::goodGay()
{
building = new Building;
}
void goodGay::visit()
{
cout << "好基友正在访问" << building->m_SittingRoom << endl;
cout << "好基友正在访问" << building->m_BedRoom << endl;
}
void test01()
{
goodGay gg;
gg.visit();
}
int main(){
test01();
system("pause");
return 0;
}
成员函数做友元
class Building;
class goodGay
{
public:
goodGay();
void visit();
void visit2();
private:
Building *building;
};
class Building
{
friend void goodGay::visit();
public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
Building::Building()
{
this->m_SittingRoom = "客厅";
this->m_BedRoom = "卧室";
}
goodGay::goodGay()
{
building = new Building;
}
void goodGay::visit()
{
cout << "好基友正在访问" << building->m_SittingRoom << endl;
cout << "好基友正在访问" << building->m_BedRoom << endl;
}
void goodGay::visit2()
{
cout << "好基友正在访问" << building->m_SittingRoom << endl;
}
void test01()
{
goodGay gg;
gg.visit();
}
int main(){
test01();
system("pause");
return 0;
}
|