C++ Day04 this指针,友元函数,重载

this指针

概念

谁调用 this 所在的函数 ,this 就存储谁的地址

特点

1, 在当前类的非静态成员函数中调用本类非静态成员时 , 默认有 this 关键字
2, 静态成员函数 , 没有 this 指针。
示例
#include <iostream>
#include <cstring>
using namespace std;
class Stu{
private:
    char name[50];
    char sex[10];
    int age;
public:
    Stu(){}
    Stu(char *n,char *s,int a):age(a)
    {
        strcpy(name,n);
        strcpy(sex,s);
    }
    void test01()
    {
    //以下两句代码效果相同
    //证明本类函数中调用本类成员默认使用this关键字
        cout << this->name << endl;
        cout << name << endl;
    }
    static void test02()
    {
    //报错,因为静态函数中没有this
        //cout << name << endl;
    }
};
int main(int argc, char *argv[])
{
    Stu s("张三","男",18);
    s.test01();
    s.test02();
    return 0;
}

使用场景

1, 局部变量与成员变量重名时 , 使用 this 区分
2, 调用本类其他成员 , 此时 this 可以省略不写
示例
#include <iostream>
#include <cstring>
using namespace std;
class Stu{
private:
    char name[50];
    char sex[10];
    int age;
public:
    Stu(){}
    Stu(char *name,char *sex,int age)
    {
    //当局部变量与成员变量重名,使用this区分
        strcpy(this->name,name);
        strcpy(this->sex,sex);
        this->age = age;
    }
    void print_info()
    {
        //调用本类成员变量
        cout << this->name << endl;
        cout << this->sex << endl;
        cout << this->age << endl;
    }
    void test()
    {
    //调用本类成员函数
        this->print_info();
    }
};
int main(int argc, char *argv[])
{
    Stu s("张三","男",18);
    s.print_info();
    s.test();
    return 0;
}

实例

使用 :*this 完成链式编程
#include <iostream>
#include <cstring>
using namespace std;
class Stu{
private:
    char name[50];
    char sex[10];
    int age;
public:
    Stu(){}
    Stu(char *name,char *sex,int age)
    {
        strcpy(this->name,name);
        strcpy(this->sex,sex);
        this->age = age;
    }
    void print_info()
    {
        cout << this->name << endl;
        cout << this->sex << endl;
        cout << this->age << endl;
    }
    Stu& eat(char *foodName)
    {
        cout << name << "吃" << foodName << endl;
        return *this;
    }
};
int main(int argc, char *argv[])
{
    Stu s("张三","男",18);
    s.eat("凉皮").eat("肉夹馍").eat("甑糕");
    return 0;
}

const修饰成员函数(了解)

特点

        const修饰的成员函数内部不能对成员数据写操作, mutable 修饰的成员数据 除外。

实例

class Stu{
private:
    char name[50];
    char sex[10];
    int age;
    mutable int score;
public:
    Stu(){}
    Stu(char *name,char *sex,int age)
    {
        strcpy(this->name,name);
        strcpy(this->sex,sex);
        this->age = age;
    }
    void print_info()
    {
        cout << this->name << endl;
        cout << this->sex << endl;
        cout << this->age << endl;
    }
    Stu& eat(char *foodName)
    {
        cout << name << "吃" << foodName << endl;
        return *this;
    }
    void test() const
    {
    //age = 10;//错误
    score = 99;//正确
    }
};

友元函数(重要)

概述

关键字 :friend
可以声明 :
        1,全局函数
        2,成员函数
        3,类
注意 :
        友元打破c++ 的封装性。一般用于运算符重载

全局友元函数

特点

可以访问其友元类的任意成员 , 包括私有成员

步骤

1, 在定义并实例全局函数
2, 在类中声明步骤 1 中的函数为友元函数
3, 步骤 1 中定义的函数 , 可以访问步骤 2 中定义的类中的所有成员
  • 25
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZHANGα

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值