超载“,” [英] overloading of ","

查看:59
本文介绍了超载“,”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码,但它没有返回我的期望,为什么?


#include< iostream>


using namespace std;


class Other

{

public:

int i;


其他(int x = 1)

{

i = x;

}


其他* operator-(){return this;}


其他&运算符+(其他t)

{

i + = ti;


返回* this;

}


其他&操作员,(其他)

{

i = oth.i;


返回* this;

}


};


int main()

{

其他o0,o1,o2(4),o3(5);


o0-> i = 100;


cout< < o0.i<< " \\\
" << o0-> i<< " \ n";


//这里它返回5而不是6为什么????????????????? />
其他牛=(o1 + o1,o3 = o2 + o1);

// ------------------

cout<< ox.i<< endl;


返回0;

}

Hi, I coded the following but It does not return what I expect, why?

#include <iostream>

using namespace std;

class Other
{
public:
int i;

Other(int x=1)
{
i = x;
}

Other *operator-() { return this;}

Other &operator+ (Other t)
{
i += t.i;

return *this;
}

Other &operator,(Other oth)
{
i = oth.i;

return *this;
}

};

int main()
{
Other o0, o1, o2(4), o3(5);

o0->i = 100;

cout << o0.i << "\n" << o0->i << "\n";

// HERE it returns 5 AND not 6 WHY ???????????????????
Other ox = (o1 + o1, o3 = o2 + o1);
// ------------------
cout << ox.i << endl;

return 0;
}

推荐答案

joshaécrit:
josh a écrit :

我编码了以下内容但它没有返回我的期望,为什么?


#include< iostream>


使用命名空间std;


class其他

{

public:

int i;


其他(int x = 1)

{

i = x;

}


其他* operator-(){return this;}


其他& operator +(其他t)

{

i + = ti;


返回* this;

}


其他&运营商,(其他)

{

i = oth.i;


返回* this;

}


};


int main( )

{

其他o0,o1,o2(4),o3(5);


o0-> i = 100;


cout<< o0.i<< " \\\
" << o0-> i<< " \ n";


//这里它返回5而不是6为什么????????????????? />
其他牛=(o1 + o1,o3 = o2 + o1);
Hi, I coded the following but It does not return what I expect, why?

#include <iostream>

using namespace std;

class Other
{
public:
int i;

Other(int x=1)
{
i = x;
}

Other *operator-() { return this;}

Other &operator+ (Other t)
{
i += t.i;

return *this;
}

Other &operator,(Other oth)
{
i = oth.i;

return *this;
}

};

int main()
{
Other o0, o1, o2(4), o3(5);

o0->i = 100;

cout << o0.i << "\n" << o0->i << "\n";

// HERE it returns 5 AND not 6 WHY ???????????????????
Other ox = (o1 + o1, o3 = o2 + o1);



因为你有5.

表达式评估:

其他牛=((o1 + o1) ,(o3 = o2 + o1));


或带名字

其他ox = o1.operator +(o1).operator,(o3.operator = (o2.operato r +(o1)));


因为o3是5,所以o1也是5而牛是5.


原因是重载运算符,与

POD运算符没有相同的优先级。是非常令人困惑的。

Because you have 5.
The expression evaluates:
Other ox = ( (o1 + o1) , (o3 = o2 + o1) );

Or with names
Other ox = o1.operator+(o1).operator,(o3.operator=(o2.operato r+(o1)));

Since o3 is 5, then o1 is also 5 and ox is 5.

The reason is overloaded operator, doesn''t have the same precedence as
POD operator,. Is is very confusing.


// ------------------

cout<< ox.i<<结束;


返回0;

}
// ------------------
cout << ox.i << endl;

return 0;
}




Michael



Michael


josh写道:
josh wrote:

我编写了以下代码,但它没有返回我的期望,为什么?


#include< iostream>


使用命名空间std;


class其他

{

public:

int i;


其他(int x = 1)

{

i = x;

}


其他* operator-(){return this;}


其他&运营商+(其他t)

{

i + = ti;


返回*这个;

}


其他&运营商,(其他)

{

i = oth.i;


返回* this;

}


};


int main()

{

其他o0,o1,o2(4),o3(5);

o0-> i = 100;


cout<< o0.i<< " \\\
" << o0-> i<< " \ n";


//这里它返回5而不是6为什么????????????????? />
其他牛=(o1 + o1,o3 = o2 + o1);

// ------------------

cout<< ox.i<< endl;


返回0;

}
Hi, I coded the following but It does not return what I expect, why?

#include <iostream>

using namespace std;

class Other
{
public:
int i;

Other(int x=1)
{
i = x;
}

Other *operator-() { return this;}

Other &operator+ (Other t)
{
i += t.i;

return *this;
}

Other &operator,(Other oth)
{
i = oth.i;

return *this;
}

};

int main()
{
Other o0, o1, o2(4), o3(5);

o0->i = 100;

cout << o0.i << "\n" << o0->i << "\n";

// HERE it returns 5 AND not 6 WHY ???????????????????
Other ox = (o1 + o1, o3 = o2 + o1);
// ------------------
cout << ox.i << endl;

return 0;
}



我怀疑这可能是未定义的行为因为+运算符具有相同的优先级,并且它们的评估顺序取决于

编译器。评估顺序是''+'',''='','',''基于优先级和

因此代码不安全。


JB

I suspect that this could be undefined behaviour as the + operators have
equal precedence and the order they are evaluated depends on the
compiler. evaluation order is ''+'', ''='', '','' based on precedence and
hence the code is not safe.

JB


Michael DOUBEZ写道:
Michael DOUBEZ wrote:

joshaécrit:
josh a écrit :

>我编写了以下代码,但它没有返回我的期望,为什么?

#include< iostream>

使用命名空间std;

课程其他
{
公开:
int i;

其他(int x = 1)
{
i = x;
}

其他* operator-(){return this;}

其他&运算符+(其他t)
{
我+ = ti;

返回*这个;
}
其他&运营商,(其他)
{
i = oth.i;

返回* this;
}

};

int main()
{
其他o0,o1,o2(4),o3(5);

o0-> i = 10 0;

cout<< o0.i<< " \\\
" << o0-> i<< " \ n";

//这里它返回5而不是6为什么??????????????????其他牛=(o1 + o1,o3 = o2 + o1);
>Hi, I coded the following but It does not return what I expect, why?

#include <iostream>

using namespace std;

class Other
{
public:
int i;

Other(int x=1)
{
i = x;
}

Other *operator-() { return this;}

Other &operator+ (Other t)
{
i += t.i;

return *this;
}

Other &operator,(Other oth)
{
i = oth.i;

return *this;
}

};

int main()
{
Other o0, o1, o2(4), o3(5);

o0->i = 100;

cout << o0.i << "\n" << o0->i << "\n";

// HERE it returns 5 AND not 6 WHY ???????????????????
Other ox = (o1 + o1, o3 = o2 + o1);



因为你有5.

表达式评估:

其他牛=((o1 + o1) ,(o3 = o2 + o1));


或带名字

其他ox = o1.operator +(o1).operator,(o3.operator = (o2.operato r +(o1)));


因为o3是5,所以o1也是5而牛是5.


原因是重载运算符,与

POD运算符没有相同的优先级。是非常令人困惑的。


Because you have 5.
The expression evaluates:
Other ox = ( (o1 + o1) , (o3 = o2 + o1) );

Or with names
Other ox = o1.operator+(o1).operator,(o3.operator=(o2.operato r+(o1)));

Since o3 is 5, then o1 is also 5 and ox is 5.

The reason is overloaded operator, doesn''t have the same precedence as
POD operator,. Is is very confusing.



这不是唯一令人困惑的事情!你对operator,()的使用是如此的b
混淆了!为什么在地球上你想要做那样的事情?

你有什么可能的理由?


在旁注,为什么不是算子? ,()与常规

'',''运算符相同的优先级?我认为所有用户定义的运算符都具有与内置函数相同的优先级



Adrian

-

================================================== ========

Adrian Hawryluk BSc。计算机科学

---------------------------------------- ------------------

专攻:UML中的OOD方法

C,C ++及更多的OOP方法

RT嵌入式编程

__--------------------------- -------------------__

----- [博客: http://adrians-musings.blogspot.com/] -----

'' - -------------------------------------------------- ----''

我的新闻组作品根据广告获得许可

Commons Attribution-Noncommercial-Share Alike 3.0 License
http://creativecommons.org/licenses/by-nc-sa/3.0/

========================================== ======== ========

That is not the only thing confusing! Your use of operator,() is the so
obfuscated! Why on Earth would you want to do something like that?
What possible reason could you have?

On a side note, why isn''t operator,() the same precedence as the regular
'','' operator? I thought that all user defined operators had the same
precedence as the builtins.
Adrian
--
================================================== ========
Adrian Hawryluk BSc. Computer Science
----------------------------------------------------------
Specialising in: OOD Methodologies in UML
OOP Methodologies in C, C++ and more
RT Embedded Programming
__--------------------------------------------------__
----- [blog: http://adrians-musings.blogspot.com/] -----
''--------------------------------------------------------''
My newsgroup writings are licensed under the Creative
Commons Attribution-Noncommercial-Share Alike 3.0 License
http://creativecommons.org/licenses/by-nc-sa/3.0/
================================================== ========


这篇关于超载“,”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆