类调用其他类成员/静态函数/this指针

2021-07-30  本文已影响0人  Sheik

环境:ide:Mac+clion

视频链接:
https://www.bilibili.com/video/BV1Hb411Y7E5?p=5

一个类是另外一个类的成员属性,构造函数和析构函数的执行顺序。

1。构造函数,先执行其他类的构造,再执行本类构造。
2。析构函数和构造函数的执行顺序正好相反。

class Phone{
public:
    string phoneNumber;
    Phone(){
        cout <<"Phone默认构造函数!"<<endl;
    }

    ~Phone(){
        cout << "Phone析构函数"<<endl;
    }

    Phone(string number):phoneNumber(number){
        cout <<"Phone有参构造函数"<<endl;
    }


};

class Person{
public:
    Phone phone;
    string m_Name;
    //再这里有一个隐示转化,Phone p_Name = pName; 这里就是通过隐示调用构造其他类有参构造函数进行。也就是上面的 Phone(string pName) : p_Name(pName) 构造函数。
    Person(string phone,string name):phone(phone),m_Name(name){
        cout <<"Person 有参构造函数执行"<<endl;
    }
    ~Person(){
        cout <<"Person 析构函数执行。"<<endl;
    }
};

    Person p("phone","皮特");
//    Phone有参构造函数
//    Person 有参构造函数执行
//    Person 析构函数执行。
//    Phone析构函数

静态成员函数:

1.所有的对象都共享一个函数。
2.静态成员函数,只能访问静态成员变量

class Person{

public:
    static void func(){
//       m_Name = "这里只能访问static 变量";
        //m_Age = 10;
        cout <<"静态函数被调用"<<endl;
    }

    void func1(){
        cout << "普通函数被调用"<<endl;
    }

    string m_Name;
    static int m_Age;
};

    Person person;
    person.func1();//这是通过对象的方式调用。
    person.func();
    Person::func();//可以直接通过类名来调用。

c++中对象模型和指针:

1.c++编译器会给每一个空对象,分配一个字节的空间.
2.只有成员变量的数据会保存在对象上,其他的都不会保存在对象上。

class Person{

public:
    //int m_A;
    static int m_B;

     void func(){
        cout <<"普通函数!"<<endl;
    }
    static void s_func(){
         cout <<"静态函数"<<endl;
     }
};
    
Person p;
cout <<sizeof (p)<<endl;//结果是1。证明只有成员变量才会保存在对象上。
class Person{

public:
    int m_A;//非静态成员变量。属于对象上。
    static int m_B;//静态成员函数,不属于对象上。

     void func(){//非静态成员函数,不属于对象上。
        cout <<"普通函数!"<<endl;
    }
    static void s_func(){//静态成员函数,不属于对象上
         cout <<"静态函数"<<endl;
     }
};

Person p;
 cout <<sizeof (p)<<endl;//这里输出是4,因为有一个int 的4个字节。

this 指针:

指向被调用成员函数所属的对象。大白话,就是谁调用它,它就指向哪个对象。如果想返回对象本身,就return *this; 对this 进行解引用。
c++ 是允许空指针调用成员函数。 只要里面没有引用this 就不会报错。 如果对this 做了判空处理,也可以调用。见例子test04()

class Person{
public:
    int m_Age;

    Person(int age){
        this->m_Age = age;//this 指向的是谁调用它就是指向谁。
        cout << "有参构造函数!"<<endl;
    }

    //年龄加法函数
    void add(const Person &person){
         this->m_Age += person.m_Age;
    }

    //这样就实现了累加,也就是可以重复调用该函数实现链式累加。
    Person &linkAdd(const Person &person){
        this->m_Age += person.m_Age;
        return *this;//返回对象本身
    }

    void showClassName(){
        cout << "显示类名Person"<<endl;
    }

    void showClassAge(){
        cout << "显示该类的年龄:"<<this->m_Age<<endl;
    }

};

void test01(){
    Person person(18);
    cout << person.m_Age<<endl;
}

void test02(){
    Person person(18);
    person.add(person);
    cout << person.m_Age<<endl;
}

void test03(){
    Person person(10);
    person.linkAdd(person).linkAdd(person).linkAdd(person);
    cout << person.m_Age<<endl;//80
}

void test04(){
    Person  * person = NULL;
    person->showClassName();//这里调用是不就报错的。 因为它里面没有使用到this指针
//    person->showClassAge();//这里会报错。因为这里用了this 指针调用。
}

//    test01();
//    test02();
//    test03();
    test04();
上一篇 下一篇

猜你喜欢

热点阅读