473,473 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

assignment to overloaded operator <<

Hello,
My question is whether it is possible to avoid assignment on the left hand
side of an overloaded operator << expression, as in the code below. Without
the assignment, the compiler complains.

class myclass
{
public static myclass operator << (myclass lhs, int rhs)
{
return(lhs);
}

public static void Main()
{
myclass x = new myclass();
int a = 1;
int b = 2;
int c = 3;
x << a << b << c;
// error CS0201: Only assignment, call, increment, decrement, and
new // object expressions can be used as a statement
}
}

So instead, I have to write:
x = x << a << b << c;
Is there a way to avoid the assignment?

Thanks

Nov 16 '05 #1
5 5084

"Ian Lazarus" <no****@nowhere.com> wrote in message
news:vD*****************@bgtnsc05-news.ops.worldnet.att.net...
Hello,
My question is whether it is possible to avoid assignment on the left hand
side of an overloaded operator << expression, as in the code below.
Without the assignment, the compiler complains.

class myclass
{
public static myclass operator << (myclass lhs, int rhs)
{
return(lhs);
}

public static void Main()
{
myclass x = new myclass();
int a = 1;
int b = 2;
int c = 3;
x << a << b << c;
// error CS0201: Only assignment, call, increment, decrement, and
new // object expressions can be used as a statement
}
}

So instead, I have to write:
x = x << a << b << c;
Is there a way to avoid the assignment?


No. Shifting operators, like most, are expected to perform some kind of work
and return a result. Most of the time a correctly defined operator should
not change the contents of the current object, instead returning a new one.
Therefore they require assignment. The only operator exceptions are
increment\decrement, which are implicitly assigned, and new, which runs
code.

What are you trying to achieve here? Shifting or C++ like streams? I don't
believe C++ like stream operators are emulateable within C#. If your class
supports some form of logical shifting you may want to look into making your
class immutable and require the assignment
Nov 16 '05 #2
Yes, the idea is to duplicate the overloading of << and >> as is often shown
in C++ for file input/output. Based on my _very_ limited knowledge of C#, it
seems that all that is necessary is to overload the operator, which it
permits. Is there some fundamental reason why << and >> shouldn't alter the
object? Who determines what a "correctly" behaving operator is? As long as
it is intuitive to the user, who cares what it does?

file << a << b << c; is certainly more convenient then file.Write(a);
file.Write(b); file.Write(c);

Thanks
Nov 16 '05 #3

"Ian Lazarus" <no****@nowhere.com> wrote in message
news:Lb****************@bgtnsc05-news.ops.worldnet.att.net...
Yes, the idea is to duplicate the overloading of << and >> as is often
shown in C++ for file input/output. Based on my _very_ limited knowledge
of C#, it seems that all that is necessary is to overload the operator,
which it permits. Is there some fundamental reason why << and >> shouldn't
alter the object? Who determines what a "correctly" behaving operator is?
As long as it is intuitive to the user, who cares what it does?

There is no fundamental reason, but there are two pretty effective ones.
One is that the language is designed to disallow operators that do not
result in reassignments. Thus, operators that make changes to an object must
still be used in an assignment.

The second is that people expect

if ((b<<a) !=0)
{

}

would not change either a or b. By making an operator change one of the
operands instead of returning a new one, you create non-obvious behaviour
and increase the risk of bugs in your code.

No operator is expected to change the value of an operand. ++, --, and the
<op>= style operators are the closest thing to an exception, and in each of
those cases, the operators are actually a combination of operator and
assignment and are expected to be result in a variables value change, not an
operand itself nessecerily changing.

Again, nothing requires that operators do not change any of their operands,
I just don't think its a good idea nor do I think that the language
particularly lends itself to doing so. file << a << b << c; is certainly more convenient then file.Write(a);
file.Write(b); file.Write(c);


Actually, I think it was one of the worst ideas in C++ ever, but thats a
different matter(makes << mean to many different things). It is convient to
a C++ user, but its utterly unclear to someone who has never used C++, and
it looks like a bit shift, not a file write.
Nov 16 '05 #4
Ian Lazarus <no****@nowhere.com> wrote:
Yes, the idea is to duplicate the overloading of << and >> as is often shown
in C++ for file input/output. Based on my _very_ limited knowledge of C#, it
seems that all that is necessary is to overload the operator, which it
permits. Is there some fundamental reason why << and >> shouldn't alter the
object? Who determines what a "correctly" behaving operator is? As long as
it is intuitive to the user, who cares what it does?

file << a << b << c; is certainly more convenient then file.Write(a);
file.Write(b); file.Write(c);


It's also less readable to those who aren't familiar with C++, of
course.

How positive are you that no-one who is only familiar with .NET will
read your code?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Ian,
As long as it is intuitive to the user, who cares what it does? Intuitive to who? You as the initial developer? Or the many developers who
inherit your code?

IMHO intuitively << does a logical shift, not a write to file! As the
guidelines below state, its "immediately obvious" what the intent of the
shift operator is. Intuitively "Write" does a write to file.

See the "Operator Overloading Usage Guidelines" in the "Design Guidelines
for Class Library Developers"
http://msdn.microsoft.com/library/de...Guidelines.asp

Hope this helps
Jay

"Ian Lazarus" <no****@nowhere.com> wrote in message
news:Lb****************@bgtnsc05-news.ops.worldnet.att.net... Yes, the idea is to duplicate the overloading of << and >> as is often
shown in C++ for file input/output. Based on my _very_ limited knowledge
of C#, it seems that all that is necessary is to overload the operator,
which it permits. Is there some fundamental reason why << and >> shouldn't
alter the object? Who determines what a "correctly" behaving operator is?
As long as it is intuitive to the user, who cares what it does?

file << a << b << c; is certainly more convenient then file.Write(a);
file.Write(b); file.Write(c);

Thanks

Nov 16 '05 #6

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

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
1
by: Jacob Foshee | last post by:
Greetings, Using MS Visual C++ .NET 2003, I have the following: template <class T> class vector3 { // ... friend ostream& operator<<(ostream&, const vector3<T>& ); // ostream operator }:
3
by: TJ | last post by:
Hi, I've been referring to many codes and books and always see that the stream insertion operators are overloaded as friends. Why is that? Are there any other overloading that need the same type...
3
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
14
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> ...
4
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...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
2
by: deepakvsoni | last post by:
I've a class in DLL which has overloaded << operator. When i use the dll in other file i get run time error when i use << operator, I get abnormal program termination error.
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.