助记
关于 this:
this 相当于 * const this; //指向不可更改
关于cstring(常量指针): 指向可以更改,指向的内容不可以更改
| string hello = "hello"; |
| string hello2 = "hello2"; |
| const char * hello = hello.c_str() |
| hello = hello.c_str(); |
指针常量
| int a1 = 10; |
| int * const a1_prt =&a1; |
| *a1_prt = 20; |
关于friend
全局函数友元
| #include "iostream" |
| #include "string" |
| using namespace std; |
| class Building{ |
| |
| friend void goodGay(Building * building); |
| public: |
| string m_SittingRoom; |
| Building(){ |
| m_SittingRoom = "客厅"; |
| m_BedRoom = "卧室"; |
| } |
| private: |
| string m_BedRoom; |
| |
| |
| }; |
| void goodGay(Building * building){ |
| cout<<building->m_SittingRoom<<endl; |
| cout<<building->m_BedRoom<<endl; |
| } |
| void test01(){ |
| Building building; |
| goodGay(&building); |
| } |
| int main(){ |
| test01(); |
| return 0; |
| } |
类友元
| #include "iostream" |
| #include "string" |
| using namespace std; |
| class Building; |
| class GoodGay{ |
| public: |
| GoodGay(); |
| ~GoodGay(); |
| void visit(); |
| private: |
| Building *building; |
| }; |
| |
| class Building{ |
| |
| friend class GoodGay; |
| public: |
| Building(); |
| void HelloVisit(); |
| public: |
| string m_SittingRoom; |
| private: |
| string m_BedRoom; |
| }; |
| |
| Building::Building(){ |
| m_SittingRoom = "客厅"; |
| m_BedRoom = "卧室"; |
| } |
| void Building::HelloVisit() { |
| cout << "HelloVisit() 函数中的m_SittingRoom = " << m_SittingRoom << endl; |
| cout << "HelloVisit() 函数中的m_BedRoom = " << m_BedRoom << endl; |
| } |
| |
| GoodGay::GoodGay(){ |
| building = new Building; |
| } |
| |
| void GoodGay::visit() { |
| cout << "GoodGay类中的visit()函数中的m_SittingRoom = " << building->m_SittingRoom <<endl; |
| cout << "GoodGay类中的visit()函数中的m_bedRoom = " << building->m_BedRoom <<endl; |
| } |
| |
| GoodGay::~GoodGay() { |
| if(building!= nullptr){ |
| delete building; |
| building = nullptr; |
| } |
| } |
| |
| void test(){ |
| auto * g = new GoodGay ; |
| g->visit(); |
| delete g; |
| } |
| |
| int main() { |
| test(); |
| return 0; |
| } |
成员函数友元
| #include "iostream" |
| |
| using namespace std; |
| class Building; |
| class GoodGay |
| { |
| public: |
| GoodGay(); |
| ~GoodGay(); |
| void visit(); |
| void visit2(); |
| private: |
| Building *building; |
| }; |
| |
| |
| class Building{ |
| friend void GoodGay::visit(); |
| public: |
| Building() |
| { |
| m_SittingRoom = "客厅"; |
| m_BedRoom = "卧室"; |
| } |
| private: |
| string m_SittingRoom; |
| string m_BedRoom; |
| }; |
| |
| |
| GoodGay::GoodGay() { |
| building = new Building; |
| |
| } |
| GoodGay::~GoodGay() { |
| if(building != NULL) |
| { |
| delete building; |
| building = nullptr; |
| } |
| } |
| void GoodGay::visit() { |
| cout<<"好基友正在访问:"<<building->m_SittingRoom<<endl; |
| cout<<"好基友正在访问:"<<building->m_BedRoom<<endl; |
| } |
| |
| void test3(){ |
| auto * g =new GoodGay; |
| g->visit(); |
| delete g; |
| } |
| |
| int main(){ |
| test3(); |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】