Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中的逗号运算符+;_C++_Class_Operator Overloading_Output - Fatal编程技术网

C++ c+中的逗号运算符+;

C++ c+中的逗号运算符+;,c++,class,operator-overloading,output,C++,Class,Operator Overloading,Output,下面是代码 #include<iostream> using namespace std; class x { int a; public : x(int t=2):a(t) {} void print (){ cout <<"value is "<<a; } x& operator,(x&a){ return *this; } }; int main(){

下面是代码

#include<iostream>

using namespace std;

class x {
    int a;
public :
    x(int t=2):a(t) {}
    void print (){
        cout <<"value is "<<a;
    }
    x& operator,(x&a){
        return *this;
    }
};

int main(){
    x a(1),b(2),c(3),d(4);
    x t=(a,b,c,d);
    t.print();
    return 0;
}
#包括
使用名称空间std;
x类{
INTA;
公众:
x(int t=2):a(t){
作废打印(){
库特
无论此表达式的求值顺序如何,最左边的操作数将始终返回,因为
this
x&运算符中,(x&instance)
指的是左边的操作数,而
instance
指的是右边的操作数

因此,它将返回
a
,您将得到打印值
1


如果没有重载逗号运算符,可能会得到
4
,因为类似
(a、b、c)的表达式
将返回最右边的操作数。

来吧,你在这里已经足够长的时间了,知道如何格式化代码…请告诉我怎么做。@akash我已经更新了格式。你可以使用“编辑”选项查看当前内容,或者单击“编辑的[时间]”链接查看差异。或者尝试自己编辑并使用“{}”图标。
x t = (a,b,c,d);