失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > c++学习13 类与对象(二)c++对象模型和this指针和友元

c++学习13 类与对象(二)c++对象模型和this指针和友元

时间:2022-11-30 15:07:02

相关推荐

c++学习13 类与对象(二)c++对象模型和this指针和友元

类和对象

c++对象模型和this指针

成员变量和成员函数分开存储

在c++中类内的成员变量和成员函数分开存储

只有非静态成员变量才属于类的对象

#include<iostream>using namespace std;class Person {};class Demo {int m_A;//非静态成员变量属于对象上};class Student {//非静态成员函数void sayHello() {}};class Work {static int a;static void sayHello() {cout << "你好呀" << endl;}};int Work::a = 10;void test01() {Person p;//在c++中,为了区别不同的空对象,编译器为其赋了一个字节的存储空间cout << sizeof(p) << endl;//1Demo d;cout << sizeof(d) << endl;//4 非静态成员变量属于对象上Student s;cout << sizeof(s) << endl;//1 非静态成员函数不在对象上Work w;cout << sizeof(w) << endl;//1 静态变量不在对象上}int main() {test01();system("pause");return 0;}

this指针

this指针指向被调用的成员函数所属的对象this指针是隐含每一个非静态成员函数内的一种指针this指针不需要定义,直接调用即可

this指针的作用

当形参和成员变量相同时,可用this指针来区分

在类的非静态成员函数中返回对象本身,可以使用return * this;

注意事项: 如果返回的只是单纯的类名,则创建的是一个新的对象,只有返回引用才是返回对象本身

#include<iostream>using namespace std;class Person {public:int age;Person(int age) {//this指向成员变量或成员函数被调用的对象this->age = age;}//如果返回的是一个引用,则返回的是本身Person& personAddAge(Person& person) {this->age += person.age;//this指向p2的指针,而*this指向p2这个对象本体return *this;}//如果返回的是一个对象,则相当于创建了一个新的对象,而本身所变的只会自身操作函数一次,后续为新的对象再进行函数内操作Person personAddAge1(Person& person) {this->age += person.age;//this指向p2的指针,而*this指向p2这个对象本体return *this;}};void test01() {Person p(18);cout << "p的年龄为" << p.age << endl;}void test02() {Person p1(10);Person p2(10);Person p3(10);p2.personAddAge(p1).personAddAge(p1).personAddAge(p1);p3.personAddAge1(p1).personAddAge1(p1).personAddAge1(p1);cout << "p2的年龄为:" << p2.age << endl;//40cout << "p3的年龄为:" << p3.age << endl;//20}int main() {test01();//18test02();system("pause");return 0;}

空指针访问成员函数

c++中空指针也是可以调用this指针的,但是也要注意有没有用到this指针

如果用到this指针,需要加以判断保证代码的健壮性

#include<iostream>using namespace std;class Person {public:int m_Age;void showClass() {cout << "这是小学一年级" << endl;}void showAge() {//对this做判空处理,增强代码的健壮性,防止空指针带来的异常if (this == NULL) {return;}cout << "你的年龄" << this->m_Age << endl;}};void test01() {//创建一个空指针Person* p = NULL;p->showClass();//空指针可以调用成员函数p->showAge();//由于空指针直接调用导致程序崩溃 解决方案,在成员函数中,对this多加一层判断}int main() {test01();system("pause");return 0;}

const修饰成员函数

常函数

成员函数后加上const后我们称这个函数为常函数常函数内不可以修改成员属性只有在成员属性声明时加上关键字mutable,才可以在常函数内进行修改

常对象

声明对象前加const,称声明对象为常对象常对象只能调用常函数

常函数修改成员属性报错示例:

常对象修改成员属性报错示例:

常对象只能调用常函数

常函数和常对象调用示例代码:

#include<iostream>using namespace std;class P {public:int m_A;mutable int m_B; // mutable 修饰的成员属性可以在常函数进行修改//常函数void changeValue() const{//this->m_A = 200;m_B = 20;}void func() {}};void test01() {P p;p.changeValue();}void test02() {//在对象前加上关键字const修饰,变为常对象const P p;//p.m_A = 20;p.m_B = 30;//被mutable修饰的成员属性,在常对象下也可以进行修改//p.func(); //常对象不可以调用普通的成员函数,因为普通的成员函数可以修改成员属性p.changeValue();}int main() {test01();test02();system("pause");return 0;}

友元

目的

让一个函数或者一个类访问另一个类的私有成员

关键字

friend

实现

全局函数做友元类做友元成员函数做友元

1 全局函数做友元需要在对应类中声明

#include <iostream>using namespace std;//全局函数做友元class Building {//全局函数goodFriend()作为Building类的好朋友,可以访问Building的私有成员friend void goodFriend(Building* b);public:Building() {m_LivingRoom = "客厅";m_BedRoom = "卧室";}public:string m_LivingRoom;private:string m_BedRoom;};//全局函数void goodFriend(Building * b) {cout << "好朋友正在访问" << b->m_LivingRoom << endl;cout << "好朋友正在访问" << b->m_BedRoom << endl; //访问私有成员}void test01() {Building b;goodFriend(&b);}int main() {test01();system("pause");return 0;}

2 类做友元也需要在对应类内部声明

#include <iostream>using namespace std;//类做友元class Building {//GoodFriend作为Building的友元,可以访问Building的私有成员friend class GoodFriend;public:Building();public:string m_LivingRoom;private:string m_BedRoom;};class GoodFriend {public:GoodFriend();void visit(); //参观函数,用来访问Building的成员Building* b;};//类外实现成员函数Building::Building() {m_LivingRoom = "客厅";m_BedRoom = "卧室";}GoodFriend::GoodFriend() {b = new Building;}void GoodFriend::visit() {cout << "好朋友正在访问" << b->m_LivingRoom << endl;cout << "好朋友正在访问" << b->m_BedRoom << endl;}void test01() {GoodFriend g;g.visit();}int main() {test01();system("pause");return 0;}

3 成员函数做友元

这里解释一个问题,为什么不能把Buiding写在GoodFriend上,为什么会报错。

因为在Building中声明了visit()是Building的友元,固然可以在Building前面提前声明GoodFriend类,但不能在类外声明方法,故将GoodFriend声明在Building前面,并不能获取到实际的方法,在定义方法时,编译器无法把visit()这个成员函数当作Building的友元函数,故无法访问Building类的私有成员(个人理解,如果有更好的理解欢迎评论)

#include <iostream>using namespace std;//成员函数做友元class Building;class GoodFriend {public:GoodFriend();void visit(); //参观函数,用来访问Building的成员void visit2(); //参观函数2,不能访问Building的私有成员private:Building* b;};class Building {friend void GoodFriend::visit();public:Building();public:string m_LivingRoom;private:string m_BedRoom;};//类外实现成员函数Building::Building() {m_LivingRoom = "客厅";m_BedRoom = "卧室";}GoodFriend::GoodFriend() {b = new Building;}void GoodFriend::visit() {cout << "1好朋友正在访问" << b->m_LivingRoom << endl;cout << "1好朋友正在访问" << b->m_BedRoom << endl;}void GoodFriend::visit2() {cout << "2好朋友正在访问" << b->m_LivingRoom << endl;/*cout << "好朋友正在访问" << b->m_BedRoom << endl;*/}void test01() {GoodFriend g;g.visit();g.visit2();}int main() {test01();system("pause");return 0;}

如果觉得《c++学习13 类与对象(二)c++对象模型和this指针和友元》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。