C/C++ 重载运算符之中括号[]、小括号()

1为什么函数参数要用const定义

回答: 参数使用const定义是为了确保在函数内部不会修改传入的参数的值。当函数参数使用const修饰时,意味着函数内部不能修改该参数的值,从而保证了参数的只读性。这样做的好处是可以避免意外的修改参数值导致的错误,并增加代码的可读性和可维护性。另外,使用const修饰参数还可以接受常量参数和变量参数,提高了函数的灵活性。

2字符串函数strcmp使用

int strcmp (const char* str1, const char* str2);
strcmp比较两个字符串的大小,一个字符一个字符比较,按ASCLL码比较
标准规定:
第一个字符串大于第二个字符串,则返回大于0的数字
第一个字符串等于第二个字符串,则返回0
第一个字符串小于第二个字符串,则返回小于0的数字

3C/C++ 重载运算符之[]

“[]”在c/c++一般是实现取数组元素的值操作,”[]”内的数值为数组的下标,数组的下标为整数,如ar[0]、ar[7]。现在要实现的功能是重载”[]”使其支持下标是字符串类型的,如ar[“1st”],怎么做?
  重载操作符的原则是不能改变操作符的原有语义和操作数的个数,”[]”用于取元素的值,且只有一个操作数,为括号内的值,这个是不可被改变的,但是括号内的值是针对该数组而操作的,所以”[]”操作符肯定有一个数组对象,这也就决定了对”[]”的重载实现的函数只能是类的成员函数,因为类的成员函数具有this指针,它可以指向对象本身(对象可以内含一个数组嘛)。

———————————————— 版权声明:本文为CSDN博主「mybright_」的原创文章,遵循CC 4.0
BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_29344757/article/details/76855218

#include<iostream>
#include<cstdio>

using namespace std;

class cls
{
private:
    int ar[6];

public:
    cls();
    int& operator[] (int i);    //重载"[]"操作符
    int& operator[](const char* str);
};

cls::cls()
{
    int i;

    for (i = 0; i < 6; i++)
    {
        ar[i] = i + 1;
    }
}

int& cls::operator[] (int i)    //返回引用,这样才可以对返回值赋值
{
    return ar[i];
}

int& cls::operator[](const char* str)
{
    // TODO: 在此处插入 return 语句
    if (!strcmp("1st", str)) {
        return ar[0];
    }
    if (!strcmp("2nd", str))return ar[1];
    if (!strcmp("3th", str))return ar[2];
    if (!strcmp("4th", str))return ar[3];
    if (!strcmp("5th", str))return ar[4];
}

int main(void)
{
    cls c;

    printf("c[1] = %d\n", c[1]);

    c[4] = 16;

    printf("c[4] = %d\n", c[4]);
    c["1st"] = 100;

    printf("c[1st] =  %d\n", c["1st"]);
    printf("c[2nd] =  %d\n", c["2nd"]);
    printf("c[3th] =  %d\n", c["3th"]);


    return 0;
}

结果:
c[1] = 2
c[4] = 16
c[1st] = 100
c[2nd] = 2
c[3th] = 3

4C/C++ 重载运算符之()

函数调用运算符 () 可以被重载用于类的对象。当重载 () 时,您不是创造了一种新的调用函数的方式,相反地,这是创建一个可以传递任意数目参数的运算符函数。

#include <iostream>
using namespace std;

class Distance
{
private:
    int feet;             // 0 到无穷
    int inches;           // 0 到 12
public:
    // 所需的构造函数
    Distance() {
        feet = 0;
        inches = 0;
    }
    Distance(int f, int i) {
        feet = f;
        inches = i;
    }
    // 重载函数调用运算符
    Distance operator()(int a, int b, int c)
    {
        Distance D;
        // 进行随机计算
        D.feet = a + c + 10;
        D.inches = b + c + 100;
        return D;
    }
    // 显示距离的方法
    void displayDistance()
    {
        cout << "F: " << feet << " I:" << inches << endl;
    }
    void operator()() {
        cout << "\n你好,小括号!\n";
    }

};
int main()
{
    Distance D1(11, 10), D2;

    cout << "First Distance : ";
    D1.displayDistance();

    D2 = D1(10, 10, 10); // invoke operator()
    cout << "Second Distance :";
    D2.displayDistance();
    D2();

    return 0;
}

结果:

First Distance : F: 11 I:10
Second Distance :F: 30 I:120

在main()函数中,cc是一个类,但是”cc();”这样的语法却是函数调用,在项目中这样的写法可以避免代码出现函数指针,至于在什么场合会使用到这种做法,估计会是在稍微大型的c++软件系统才需要吧,我尚未遇到,先了解有这么一种用法。

可以看出,c++的许多操作已经可以代替指针的使用了,这也难怪,在c++之后的语言,如java它不存在指针这个东西了。

———————————————— 版权声明:本文为CSDN博主「mybright_」的原创文章,遵循CC 4.0
BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_29344757/article/details/76855218

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值