类中的函数重载

类中的函数重载

函数重载

  • 函数重载的本质是相互独立的不同函数
  • C++中通过函数名和函数参数确定函数调用
  • 无法直接通过函数名得到重载函数的入口地址
  • 函数重载必然发生在同一作用域中

类中成员函数重载

  • 重载函数的本质为多个不同的函数
  • 函数名和参数列表是唯一标识
  • 函数重载必须发生在同一作用域中

深度的意义

  • 重载的意义
    • 通过函数名对函数功能进行提示
    • 通过参数列表对函数用法进行提示
    • 扩展系统中已经存在的函数功能
#include <stdio.h>
#include <string.h>
char* strcpy(char* buf,const char* str)
{
    return strncpy(buf,str,sizeof(buf)-1);
}
int main(int argc, char const *argv[])
{
    const char* s = "CrazyPigAndCat";
    char buf[10] = {0};
    strcpy(buf,s);
    // strncpy(buf,s,sizeof(buf)-1);
    printf("%s\n",buf);
    return 0;
}

小结

  • 类的成员函数之间可以进行重载
  • 重载必须发生在同一个作用域中
  • 全局函数和成员函数不能构成重载关系
  • 重载的意义在于扩展已经存在的功能

操作符重载

  • C++中的重载能够扩展操作符的功能
  • 操作符的重载以函数的方式进行
  • 本质
    • 用特殊形式的函数扩展操作符的功能

操作符重载的使用

  • 通过operator关键字可以定义特殊的函数
  • operator的本质是通过函数重载操作符
  • 语法
Type operator Sign(const Type p1,const Type p2)
{
  Type ret;
  return ret;
}
> Sign为系统中预定义的操作符
#include <stdio.h>

class Complex
{
private:
    int a;
    int b;
public:
    Complex(int a = 0,int b = 0)
    {
        this->a = a;
        this->b = b;
    }
    int getA();
    int getB();
    friend Complex operator + (const Complex p1,const Complex p2);
};

int Complex::getA()
{
    return a;
}

int Complex::getB()
{
    return b;
}

Complex operator + (const Complex p1,const Complex p2)
{
    Complex ret;
    ret.a = p1.a+p2.a;
    ret.b = p1.b+p2.b;

    return ret;
}

int main(int argc, char const *argv[])
{
    Complex c1(1,2);
    Complex c2(3,4);
    Complex c3 = c1+c2;
    printf("c3.a = %d , c3.b = %d\n",c3.getA(),c3.getB());
    return 0;
}

  • 可以将操作符重载函数定义为类的成员函数
    • 比全局操作符重载函数少一个参数(左操作数)
    • 不需要依赖友元就可以完成操作符重载
    • 编译器优先在成员函数中寻找操作符重载函数
class Type
{
  public:
    Type operator Sign(const Type& p)
    {
      Type ret;
      return ret;
    }
};
#include <stdio.h>

class Complex
{
private:
    int a;
    int b;
public:
    Complex(int a = 0,int b = 0)
    {
        this->a = a;
        this->b = b;
    }
    int getA();
    int getB();
    Complex operator + (const Complex p1)
    {
    Complex ret;
    ret.a = p1.a+this->a;
    ret.b = p1.b+this->b;
    return ret;
    }

};

int Complex::getA()
{
    return a;
}

int Complex::getB()
{
    return b;
}

int main(int argc, char const *argv[])
{
    Complex c1(1,2);
    Complex c2(3,4);
    Complex c3 = c1+c2;
    printf("c3.a = %d , c3.b = %d\n",c3.getA(),c3.getB());
    return 0;
}

小结

  • 操作符重载是C++的强大特性之一
  • 操作符重载的本质是通过函数扩展操作符的功能
  • operator关键字是实现操作符重载的关键
  • 操作符重载遵循相同的函数重载规则
  • 全局函数和成员函数都可以实现对操作符的重载

数组操作符的重载

字符串类的兼容性

  • string类最大限度的考虑了C字符串的兼容性
  • 可以按照使用C字符串的方式使用string对象

被忽略的事实

  • 数组访问符是C/C++中的内置操作符
  • 数组访问符的原生意义是数组访问和指针运算

数组访问操作符([])

  • 只能通过类的成员函数重载
  • 重载函数能且仅能使用一个参数
  • 可以定义不同参数的多个重载函数
/*
 * @Author: your name
 * @Date: 2021-10-19 17:16:30
 * @LastEditTime: 2021-10-20 09:31:54
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /WorkSpace/C++/12C++中的重载/2.cpp
 */

#include <iostream>
#include <string>

using namespace std;

class Test
{
private:
    int a[5];
public:
    int& operator [] (int i)
    {
        return a[i];
    }
    int& operator [] (const string& s)
    {
        if(s == "1th")
        {
            return a[0];
        }
        else if(s == "2nd")
        {
            return a[1];
        }
        else if(s == "3rd")
        {
            return a[2];
        }
        else if(s == "4th")
        {
            return a[3];
        }
        else if(s == "5th")
        {
            return a[4];
        }
        else
        {
            return a[0];
        }
    }
    int length()
    {
        return 5;
    }
};

int main(int argc, char const *argv[])
{
    Test t;
    for(int i =0;i<t.length();i++)
    {
        t[i] = i;
    }
    for (size_t i = 0; i < t.length(); i++)
    {
        cout << t[i] << endl;
    }
    cout << endl;
    
    cout << t["5th"] << endl;
    cout << t["4th"] << endl;
    cout << t["3rd"] << endl;
    cout << t["2nd"] << endl;
    cout << t["1th"] << endl;
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值