473,699 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overload operator<<

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

Nov 17 '05 #1
25 2845
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.goo glegroups.com.. .
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

Nov 17 '05 #2

CSharper wrote:
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.goo glegroups.com.. .
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


Nov 17 '05 #3
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.goo glegroups.com.. .

CSharper wrote:
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.goo glegroups.com.. .
> 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
>

Nov 17 '05 #4
Conditions for overloading shiftoperators( << and >>) in C#:
The type of the first operand must always be the type containing the
operator declaration.
The type of the second operand must always be int.
"Steve Richter" <St************ @gmail.com> schrieb im Newsbeitrag
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
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

Nov 17 '05 #5
Also overloadable are following unary operators: + - ! ~ ++ -- true false.
The operators true and false aren't called explicitly in code, bat are used
to indirectly overload the operators && || ?:

Christof

"CSharper" <do**@botherme. com> schrieb im Newsbeitrag
news:PN******** ************@bi gnews4.bellsout h.net...
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.goo glegroups.com.. .
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


Nov 17 '05 #6
Compund assignment operators are overloaded indireclty.
f.e. overloading binary + also overloads +=

Christof

"CSharper" <do**@botherme. com> schrieb im Newsbeitrag
news:Ob******** ************@bi gnews4.bellsout h.net...
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.goo glegroups.com.. .

CSharper wrote:
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.goo glegroups.com.. .
> 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
>


Nov 17 '05 #7

Christof Nordiek wrote:
Conditions for overloading shiftoperators( << and >>) in C#:
The type of the first operand must always be the type containing the
operator declaration.
The type of the second operand must always be int.
big mistake by Microsoft!

operator<< is a great way to indicate that you are adding one object to
the collection of another.

What about operator+= ? Can I overload it?
collection += object ;

-Steve

"Steve Richter" <St************ @gmail.com> schrieb im Newsbeitrag
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
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


Nov 17 '05 #8
> big mistake by Microsoft!
operator<< is a great way to indicate that you are adding one object to
the collection of another. No mistake by Microsoft!
Method IList.Add is a great way to indicate that you are adding one object
to the collection of another
What about operator+= ? Can I overload it?
collection += object ;

Yes, you can, and I believe that if you read what somenon has written a few
posts below, you would find out the same ;-)
Nov 17 '05 #9

Lebesgue wrote:
big mistake by Microsoft!
operator<< is a great way to indicate that you are adding one object to the collection of another. No mistake by Microsoft!
Method IList.Add is a great way to indicate that you are adding one

object to the collection of another
puh!
table << row ;
is much cleaner looking than:
table.Add( row ) ;
What about operator+= ? Can I overload it?
collection += object ; Yes, you can, and I believe that if you read what somenon has written

a few posts below, you would find out the same ;-)


Show me the code!

As I understand it, operator+= will be provided by the c# compiler as
an abbreviated form of
public static ReturnType operator+( arg1, arg2 ) ;

so you would have to provide operator+ as a way to add an object to a
collection:
table = table + row ;

which is not very intuitive. Adding a row to a table does not create a
new table object.

-Steve

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4336
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; } This overload work but I have a curiosty If I try to approch to the name or to the surname or to the dateofbirth in this way i receive error: ostream & operator<<(ostream &out, Person &p){ out<<p.name<<" "<<p.surname<<", "<<p.dateofbirth<<endl;
4
2056
by: bluekite2000 | last post by:
Here A is an instantiation of class Matrix. This means whenever user writes Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2 //and fills it up w/ random value cout<<A; the following will be printed A 3 2
2
2177
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const Debug_Level ddl;
7
14916
by: glen | last post by:
Hi. I'm using GCC 4.1.1, which I mention since I don't know if this is a compiler issue, or me not understanding some subtlety in the standard. The code below compiles fine under vc++, but I'm having trouble using gcc. It's just a templated container output. /** outputs elements of a vector in the form '{el1,el2...elEnd}'
11
3550
by: fungus | last post by:
I have some classes which have a "writeTo()" function but no operator<<. I want to fix it so that they all have an operator<< (for consistency). Can I do something like this: template <class T> DataDest& operator<<(DataDest& d, const T& t) { t.writeTo(d);
5
3431
by: David | last post by:
Hi all, I am trying to overload the < operator, but get warning class Windowinfo { protected: HWND wndhandle; //the window handle int wndId; //the window Id
0
8704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9187
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8936
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7776
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2360
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.