C++ 光速入门指南day15

静态成员

静态成员就是在成员变量和成员函数前加上关键字static,称为静态成员

静态成员分为:

  • 静态成员变量
    • 所有对象共享同一份数据(具有记忆作用)
    • 在编译阶段分配内存
    • 类内声明,类外初始化
#include <iostream>
#include <cstring>
using namespace std;
class Person{

public:
   // 特点:
   // 在编译阶段分配内存
   // 类内声明 类外初始化
   // 所有对象共享同一份数据
   static int m_A; //静态成员变量
private:
   static int m_B; //静态成员变量也是有访问权限的

};
// :: 作用域符
int Person::m_A = 10;
int Person::m_B = 10;
int main() {
   // 通过类名进行访问
   cout << "m_A = " << Person::m_A << endl;
//    cout << "m_A = " << Person::m_B << endl; // 权限限制
   // 通过对象
   Person p1;
   p1.m_A = 100;
   cout << "p1.m_A  = " << p1.m_A  << endl;

   Person p2;
   p2.m_A = 200;
   cout << "p1.m_A  = " << p1.m_A  << endl;
   cout << "p1.m_A  = " << p2.m_A  << endl; // 共享一份数据

}

  • 静态成员函数
    • 所有对象共享同一个函数
    • 静态成员函数只能访问静态成员变量
#include <iostream>
#include <cstring>
using namespace std;
class Person{

public:
//    - 静态成员函数
//    - 所有对象共享同一个函数
//    - 静态成员函数只能访问静态成员变量
  static void func(){
       cout<< "func is called"<< endl;
       m_A = 100;
//        m_B = 10; // 静态成员只能访问静态变量
   }
   int m_B; // 属于对象
   static int m_A;  // 属于类
private:
   // 静态成员函数是有访问权限的
   static void func2(){
       cout<< "func2 is called"<< endl;
   }
};
int Person::m_A = 10;

int main() {
   // 通过对象调用
//    Person p1;
//    p1.func();
//    cout << p1.m_A <<endl;
//    Person::func();
   cout <<Person::m_A <<endl;
//    cout <<Person::func2() <<endl; // 私有权限访问不到

}

C++对象模型和this指针

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

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

#include <iostream>
#include <cstring>
using namespace std;
class Person{
public:
    // 非静态成员变量占对象空间
    int mA;
    // 静态成员变量不 占对象空间
    static int mB;
public:
    Person(){
        mA=0;
    }
    // 函数也不不占对象空间, 所有对象共享一个函数实例
    void func(){
        cout << "mA = " << this->mA << endl;
    }
    // 静态成员函数不 占对象空间
    static void sfunc(){
        cout << "mB = " << mB << endl;
    }

};
int main() {
    cout << sizeof(Person)<<endl;
    Person p1;
    p1.func();
}

每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码, 这一块代码是如何区分那个对象调用自己的呢, c++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象

this指针是隐含每一个非静态成员函数内的一种指针

this指针不需要定义,直接使用即可

this指针的用途:

  • 当形参和成员变量同名时,可用this指针来区分
  • 在类的非静态成员函数中返回对象本身,可使用return *this
#include <iostream>
#include <cstring>
using namespace std;
class Person{

public:
    int age ;
    Person(int age){
        // 形参和成员变量同名时,可用this指针来区分
        this->age = age;
    }
    // 返回对象本身
    Person& personAddPerson(Person p){
        // 将传进来的人的年龄和当前这个人的年龄相加
        this->age += p.age;
        return *this;
    }

};
void test01(){
    Person p1(10);
    cout << "p1.age = " << p1.age << endl;
    Person p2(100);
//    p2.personAddPerson(p1);
    //链式调用
    // 因为调用之后返回的就是Person对象 所以会有personAddPerson()
    p2.personAddPerson(p1).personAddPerson(p1).personAddPerson(p1);
    cout << "p2.age = " << p2.age << endl;

}
int main() {
    test01();
}

const修饰成员函数

常函数:

  • 成员函数后加const后我们称为这个函数为常函数
  • 常函数内不可以修改成员属性
  • 成员属性声明时加关键字mutable后,在常函数中依然可以修改

常对象:

  • 声明对象前加const称该对象为常对象
  • 常对象只能调用常函数
#include <iostream>
#include <cstring>
using namespace std;
class Person{
public:
    int age ;
    Person(){
        m_A = 0;
        m_B = 0;
    }
    // 常函数
    //this指针的本质是一个指针常量,指针的指向不可修改
    //如果想让指针指向的值也不可以修改,需要声明常函数
    void showPerson() const{
//        m_A = 100;
//const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量
        this->m_B=8888;
//        this->m_A=1000; 不能改
    }
    void print() {
        cout << "print()" << endl;

    }
public:
    int m_A;
    mutable int m_B;



};
void test01(){
    Person p1;
    cout << p1.m_B << endl;
    p1.showPerson();
    cout << p1.m_B << endl;

}
int main() {
//    test01();
    // 常对象
    const Person person;
    cout << person.m_B << endl;
    cout << person.m_A << endl;
//    person.m_A = 1000; 常对象不能修改成员变量的值, 但是可以访问
    person.m_B = 1000; // 常对象能修改mutable修饰的成员变量的值,
    cout << person.m_B << endl;
    // 常对象和成员函数
    person.showPerson();
    cout << person.m_B << endl;
//    person.print(); // 常对象只能调用常函数

}


空指针访问成员函数

C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针

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

#include <iostream>
#include <cstring>
using namespace std;
class Person{
public:
    int age ;
public:
    void showClassName(){
        cout << "Person Class"<< endl;
    }
    void showPerson(){
        // 空判断 return作用 返回对应的值, 结束函数
        if (this == NULL)
            return;
        cout << "age = " << age << endl;
    }


};
void test01(){
   Person *p = NULL;
   p-> showClassName(); // 空指针是可以调用成员函数
   p-> showPerson();

}
int main() {
    test01();


}

友元

友元的目的就是让一个函数或者类 访问另一个类中私有成员

生活中你的家有客厅(Public),有你的卧室(Private)

客厅所有来的客人都可以进去,但是你的卧室是私有的,也就是说只有你能进去

但是呢,你也可以允许你的好闺蜜好基友进去。

在程序里,有些私有属性 也想让类外特殊的一些函数或者类进行访问,就需要用到友元的技术

友元的关键字为 friend

友元的三种实现

  • 全局函数做友元
#include <iostream>
#include <cstring>
using namespace std;
class Building{
    // 告诉编译器 goodGay全局函数是Building类的好朋友, 可以访问类中的私有内容
    friend void goodGay(Building *building);

public:
    Building() {
        this->m_BedRoom = "woshi";
        this->m_SittingRoom = "keting";
    }

public:
    string m_SittingRoom; // 客厅
private:
    string m_BedRoom; // 卧室

};

void goodGay(Building *building){
    cout<< "goodGay ===>"<< building->m_SittingRoom<<endl;
    cout<< "goodGay ===>"<< building->m_BedRoom<<endl;
}
void test01(){
    Building b;
    goodGay(&b);

}
int main() {
    test01();

}

  • 类做友元
#include <iostream>
#include <cstring>
using namespace std;
class Building{

    friend class goodGay;

public:
    Building() ;

public:
    string m_SittingRoom; // 客厅
private:
    string m_BedRoom; // 卧室

};

class goodGay{
public:
    goodGay();
    void visit();

private:
    Building *building;
};


Building::Building() {
    this->m_BedRoom = "woshi";
    this->m_SittingRoom = "keting";
}
goodGay::goodGay() {
   building =  new Building;
}

void goodGay::visit() {
    cout << "building->m_SittingRoom -->"<<building->m_SittingRoom<< endl;
    cout << "building->m_BedRoom -->"<<building->m_BedRoom<< endl;
}

void test01(){
    goodGay gg;
    gg.visit();

}
int main() {
    test01();

}


  • 成员函数做友元

#include <iostream>
#include <cstring>
using namespace std;

class Building;
class goodGay{
public:
    goodGay();
    void visit(); //只让visit函数作为Building的好朋友,可以发访问Building中私有内容
    void visit2();

private:
    Building *building;
};
class Building{
//告诉编译器  goodGay类中的visit成员函数 是Building好朋友,可以访问私有内容
    friend void goodGay::visit();

public:
    Building() ;

public:
    string m_SittingRoom; // 客厅
private:
    string m_BedRoom; // 卧室

};

Building::Building() {
    this->m_BedRoom = "woshi";
    this->m_SittingRoom = "keting";
}
goodGay::goodGay() {
   building =  new Building;
}

void goodGay::visit() {
    cout << "building->m_SittingRoom -->"<<building->m_SittingRoom<< endl;
    cout << "building->m_BedRoom -->"<<building->m_BedRoom<< endl;
}
void goodGay::visit2() {
    cout << "building->m_SittingRoom -->"<<building->m_SittingRoom<< endl;
//    cout << "building->m_BedRoom -->"<<building->m_BedRoom<< endl;  不可以
}
void test01(){
    goodGay gg;
    gg.visit();
    gg.visit2();

}
int main() {
    test01();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值