C++第39课--逗号操作符分析,逗号操作符重载没啥意义,使用原生的逗号操作符就好

本文学习自 狄泰软件学院 唐佐林老师的 C++课程


实验1 :逗号表达式回顾
实验2:重载逗号操作符
实验3:重载逗号操作符的陷阱,逗号操作符重载没啥意义
实验4:使用原生的逗号操作符足够


在这里插入图片描述

实验1 :逗号表达式回顾

#include <iostream>
#include <string>

using namespace std;

void func(int i)
{
    cout << "func() : i = " << i << endl;
}

int main()
{   
	//数组 逗号表达式  最后一个表达式的值有效
	/*正确的初始化方式:
		    int a[3][3] = {
	        {0, 1, 2},
	        {3, 4, 5},
	        {6, 7, 8}
	    };
	*/
    int a[3][3] = {
        (0, 1, 2),
        (3, 4, 5),
        (6, 7, 8)
    };
    
    int i = 0;
    int j = 0;
    
    //逗号表达式
    while( i < 5 )    
        func(i),
    
    i++;
        
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            cout << a[i][j] << endl;
        }
    }
    
    //左值是逗号表达式,最终为 j  等价于  j=6
    (i, j) = 6;
    
    cout << "i = " << i << endl;
    cout << "j = " << j << endl;

    return 0;
}


mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ g++ 39-1.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
func() : i = 0
func() : i = 1
func() : i = 2
func() : i = 3
func() : i = 4
2
5
8
0
0
0
0
0
0
i = 3
j = 6
mhr@ubuntu:~/work/c++$ 

在这里插入图片描述

由于返回类型是 引用 所以要对b 去只读属性。

实验2:重载逗号操作符

#include <iostream>
#include <string>

using namespace std;

class Test
{
    int mValue;
public:
    Test(int i)
    {
        mValue = i;
    }
    int value()
    {
        return mValue;
    }
};

Test& operator , (const Test& a, const Test& b)
{
    return const_cast<Test&>(b);//返回值是最后有一个子表达式的值 去只读属性
}

Test func(Test& i)
{
    cout << "func() : i = " << i.value() << endl;
    
    return i;
}

int main()
{   
    Test t0(0);
    Test t1(1);
    Test tt = (t0, t1);         // Test tt = t1;
    
    cout << tt.value() << endl; // 1
    
    return 0;
}

mhr@ubuntu:~/work/c++$ g++ 39-2.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
1
mhr@ubuntu:~/work/c++$ 

实验3:重载逗号操作符的陷阱

#include <iostream>
#include <string>

using namespace std;

class Test
{
    int mValue;
public:
    Test(int i)
    {
        mValue = i;
    }
    int value()
    {
        return mValue;
    }
};

Test& operator , (const Test& a, const Test& b)
{
    return const_cast<Test&>(b);
}

Test func(Test& i)
{
    cout << "func() : i = " << i.value() << endl;
    
    return i;
}

int main()
{   
    Test t0(0);
    Test t1(1);
	//等价于  operator , (func(t0), func(t1)) ,此时参数是函数体,参数的要先计算出来,并且计算顺序不一定,func(t0), func(t1)都会被执行。
    Test tt = (func(t0), func(t1)); 
    
    cout << tt.value() << endl; // 1
    
    return 0;
}


mhr@ubuntu:~/work/c++$ g++ 39-2.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
func() : i = 1
func() : i = 0
1
mhr@ubuntu:~/work/c++$ 

分析,虽然结果是我们想要的,但是中间过程出错了,逗号表达式 变成了从右向左调用了,违背了原生的逗号表达式的语义。

在这里插入图片描述

实验4:使用原生的逗号操作符

#include <iostream>
#include <string>

using namespace std;

class Test
{
    int mValue;
public:
    Test(int i)
    {
        mValue = i;
    }
    int value()
    {
        return mValue;
    }
};
/*
Test& operator , (const Test& a, const Test& b)
{
    return const_cast<Test&>(b);
}
*/
Test func(Test& i)
{
    cout << "func() : i = " << i.value() << endl;
    
    return i;
}

int main()
{   
    Test t0(0);
    Test t1(1);
    Test tt = (func(t0), func(t1));         // Test tt = func(t1);
    
    cout << tt.value() << endl; // 1
    
    return 0;
}


mhr@ubuntu:~/work/c++$ g++ 39-2.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
func() : i = 0
func() : i = 1
1
mhr@ubuntu:~/work/c++$ 

结果是 逗号表达式中是从左向右执行,原生的逗号表达式完全可以满足我们的需要,所以重载逗号表达式完全是多此一举,还会出错。

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Linux老A

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

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

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

打赏作者

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

抵扣说明:

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

余额充值