过载运算符<< [英] overload operator<<

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

问题描述

是否可以重载<< c#中的运算符类似于它的方式

是用c ++完成的?


MyTable table = new MyTable();

MyTableRow row = new MyTableRow();

row<< new MyTableCell(" cell1 text contents");

row<< new MyTableCell(" cell2 text contents");

table<<行;


谢谢,


-Steve

is it possible to overload the << operator in c# similar to the way it
is done in c++ ?

MyTable table = new MyTable( ) ;
MyTableRow row = new MyTableRow( ) ;
row << new MyTableCell( "cell1 text contents" ) ;
row << new MyTableCell( "cell2 text contents" ) ;
table << row ;

thanks,

-Steve

推荐答案

似乎可以重载以下任何二元运算符:


+ - * /%& | ^<< >> ==!=> < > =< =


希望这会有所帮助,


Dan Cox

免责声明:不要把它带到银行,我只是想帮忙。


Steve Richter <圣************ @ gmail.com>在消息中写道

news:11 ********************** @ z14g2000cwz.googlegr oups.com ...
It seems possible to overload any of the following binary-operators:

+ - * / % & | ^ << >> == != > < >= <=

Hope this helps,

Dan Cox
DISCLAIMER: Don''t take it to the bank, I''m just trying to help.

"Steve Richter" <St************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
是否可以重载<< c#中的运算符类似于用c ++完成的方式吗?

MyTable table = new MyTable();
MyTableRow row = new MyTableRow();
row << new MyTableCell(" cell1 text contents");
row<< new MyTableCell(" cell2 text contents");
table<<行;

谢谢,

-Steve
is it possible to overload the << operator in c# similar to the way it
is done in c++ ?

MyTable table = new MyTable( ) ;
MyTableRow row = new MyTableRow( ) ;
row << new MyTableCell( "cell1 text contents" ) ;
row << new MyTableCell( "cell2 text contents" ) ;
table << row ;

thanks,

-Steve





CSharper写道:

CSharper wrote:
似乎可以重载以下任何二元运算符:

+ - * /%& | ^<< >> ==!=> < > =< =

希望这会有所帮助,

Dan Cox
免责声明:不要把它带到银行,我只是在尝试帮助。


信任但验证,对吗?我很感激帮助!


我认为它不会像我想要的那样工作。我收到编译错误

说运算符的第一个参数<<必须是一个int。如上所示,

的位置向左移动。


有趣的是为什么语言不允许完全像运算符这样的c ++

重载。这真是一个很好的功能。


谢谢,


-Steve


史蒂夫里氏" <圣************ @ gmail.com>在消息中写道
新闻:11 ********************** @ z14g2000cwz.googlegr oups.com ...
It seems possible to overload any of the following binary-operators:

+ - * / % & | ^ << >> == != > < >= <=

Hope this helps,

Dan Cox
DISCLAIMER: Don''t take it to the bank, I''m just trying to help.
trust but verify, right? I appreciate the help!

I dont think it will work the way I want it to. I got a compile error
saying the first argument for operator<< had to be an int. as in, the
number of positions to shift to the left.

Funny why the language does not allow full c++ like operator
overloading. It really is a good feature.

thanks,

-Steve


"Steve Richter" <St************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
是否有可能重载<< c#中的运算符类似于
的方式用c ++完成?

MyTable table = new MyTable();
MyTableRow row = new MyTableRow();
row << new MyTableCell(" cell1 text contents");
row<< new MyTableCell(" cell2 text contents");
table<<排;

谢谢,

-Steve
is it possible to overload the << operator in c# similar to the way it is done in c++ ?

MyTable table = new MyTable( ) ;
MyTableRow row = new MyTableRow( ) ;
row << new MyTableCell( "cell1 text contents" ) ;
row << new MyTableCell( "cell2 text contents" ) ;
table << row ;

thanks,

-Steve






来自MSDN:C#与C ++重载


与C ++相比,C#允许重载更少的运算符。有两套

套的限制。首先,成员访问,成员调用(即,

函数调用),赋值和new不能超载,因为运行时定义了这样的操作。


其次,运营商如&&," || ","?:"和复合

赋值运算符,例如" + ="不能超载,因为增加的

复杂性不值得获益。


Dan Cox


"史蒂夫里氏" <圣************ @ gmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
From MSDN: C# vs. C++ Overloading

Compared to C++, C# allows overloading of fewer operators. There are two
sets of restrictions. First, member access, member invocation (that is,
function calling), assignment, and "new" cannot be overloaded because such
operations are defined by the runtime.

Second, operators such as "&&", "||", "?:", and compound
assignment operators such as "+=" cannot be overloaded because the added
complexity is not worth the benefit.

Dan Cox

"Steve Richter" <St************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

CSharper写道:

CSharper wrote:
似乎有可能超载以下任何二元运算符:

+ - * /%& | ^<< >> ==!=> < > =< =

希望这会有所帮助,

Dan Cox
免责声明:不要把它带到银行,我只是在尝试帮助。
It seems possible to overload any of the following binary-operators:

+ - * / % & | ^ << >> == != > < >= <=

Hope this helps,

Dan Cox
DISCLAIMER: Don''t take it to the bank, I''m just trying to help.



信任但验证,对吧?我很感激帮助!

我认为它不会像我想要的那样工作。我得到一个编译错误
说运算符的第一个参数<<必须是一个int。就像在,向左移动的位置数量。

有趣的是为什么语言不允许像运算符那样的完整c ++重载。这真是一个很好的功能。

谢谢,

-Steve



trust but verify, right? I appreciate the help!

I dont think it will work the way I want it to. I got a compile error
saying the first argument for operator<< had to be an int. as in, the
number of positions to shift to the left.

Funny why the language does not allow full c++ like operator
overloading. It really is a good feature.

thanks,

-Steve


史蒂夫里氏" <圣************ @ gmail.com>在消息中写道
新闻:11 ********************** @ z14g2000cwz.googlegr oups.com ...

"Steve Richter" <St************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
>是否有可能重载<< c#中的运算符类似于它的方式>用c ++完成?
>
> MyTable table = new MyTable();
> MyTableRow row = new MyTableRow();
>行<< new MyTableCell(cell1 text contents);
>行<< new MyTableCell(cell2 text contents);
>表<<行;
>
>谢谢,
>
> -Steve
>
> is it possible to overload the << operator in c# similar to the way it > is done in c++ ?
>
> MyTable table = new MyTable( ) ;
> MyTableRow row = new MyTableRow( ) ;
> row << new MyTableCell( "cell1 text contents" ) ;
> row << new MyTableCell( "cell2 text contents" ) ;
> table << row ;
>
> thanks,
>
> -Steve
>



这篇关于过载运算符&lt;&lt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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