operator-overloading

Java operator overload

Coming from C++ to Java, the obvious unanswered question is why not operator overload. On the web some go about: "it's clearly obfuscated and complicate maintenance" but no one really elaborates that further (I completely disagree, actually). Other people pointed out that some objects do have an overload (like String operator +) but t...

Best way to handle null when writing equals operator

When I overload the == operator for objects I typically write something like this: public static bool operator ==(MyObject uq1, MyObject uq2) { if (((object)uq1 == null) || ((object)uq2 == null)) return false; return uq1.Field1 == uq2.Field1 && uq1.Field2 == uq2.Field2; } If you don't down-cast to object the function recurses in...

Why Does Ruby Only Permit Certain Operator Overloading

In Ruby, like in many other OO programming languages, operators are overloadable. However, only certain character operators can be overloaded. This list may be incomplete but, here are some of the operators that cannot be overloaded: !, not, &&, and, ||, or ...

What is "Best Practice" For Comparing Two Instances of a Reference Type?

I came across this recently, up until now I have been happily overriding the equality operator (==) and/or Equals method in order to see if two references types actually contained the same data (i.e. two different instances that look the same). I have been using this even more since I have been getting more in to automated testing (comp...

How do I prevent a class from being allocated via the 'new' operator? (I'd like to ensure my RAII class is always allocated on the stack.)

I'd like to ensure my RAII class is always allocated on the stack. How do I prevent a class from being allocated via the 'new' operator? ...

Is it possible to prevent stack allocation of an object and only allow it to be instiated with 'new'?

Is it possible to prevent stack allocation of an object and only allow it to be instiated with 'new' on the heap? ...

overloading __init__ in python

Let's say I have a class that has a member called data which is a list. I want to be able to initialize the class with, for example, a filename (which contains data to initialize the list) or with an actual list. What's your technique for doing this? Do you just check the type by looking at __class__? Is there some trick I might be...

Solution for overloaded operator constraint in .NET generics

What would I do if I want to have a generic method that only accepts types that have overloaded an operator, for instance the subtraction operator. I tried using an interface as a constraint but interfaces can't have operator overloading. What is the best way to achieve this? ...

Namespaces and Operator Overloading in C++

When authoring a library in a particular namespace, it's often convenient to provide overloaded operators for the classes in that namespace. It seems (at least with g++) that the overloaded operators can be implemented either in the library's namespace: namespace Lib { class A { }; A operator+(const A&, const A&); } // namespace Lib ...

Operator Overloading with C# Extension Methods

I'm attempting to use extension methods to add an operater overload to the C# StringBuilder class. Specifically, given StringBuilder sb, I'd like sb += "text" to become equivalent to sb.Append("text"); Here's the syntax for creating an extension method for StringBuilder: public static class sbExtensions { public static StringBuild...

function overloading fail: why did these operators clash?

Hi, I've got a big big code base that includes two main namespaces: the engine and the application. The engine defines a vector3 class as a typedef of another vector3 class, with equality operators that sit in the engine namespace, not in the vector3 class. I added a class to the application that also had equality operators in the a...

Should operator<< be implemented as a friend or as a member function?

That's basically the question, is there a "right" way to implemente operator<< ? Reading this I can see that something like: friend bool operator<<(obj const& lhs, obj const& rhs); Is prefered to something like ostream& operator<<(obj const& rhs); But I can't quite see why should I use one or the other. My personal case is: frie...

How do I overload the square-bracket operator in C#?

DataGridView, for example, lets you do this: DataGridView dgv = ...; DataGridViewCell cell = dgv[1,5]; but for the life of me I can't find the documentation on the index/square-bracket operator. What do they call it? Where is it implemented? Can it throw? How can I do the same thing in my own classes? ETA: Thanks for all the quic...

How to overload operator<< that doesn't take or return ostream

Original Question I am writting a logging class where the goal is to be able to do this: // thread one Logger() << "Some string" << std::ios::hex << 45; // thread two Logger() << L"Some wide string" << std::endl; Currently my Logger header looks something like this: #pragma once; #include <ostream> class Logger { public: Log...

Is there a workaround for overloading the assignment operator in C#?

Unlike C++, in C# you can't overload the assignment operator. I'm doing a custom Number class for arithmetic operations with very large numbers and I want it to have the look-and-feel of the built-in numerical types like int, decimal, etc. I've overloaded the arithmetic operators, but the assignment remains... Is there a workaround fo...

Why override operator() ?

In the Boost Signals library, they are overloading the () operator. Is this a convention in C++? For callbacks, etc.? I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led to confusion for me. Any insight as to the reason for this overload? ...

Possible to override null-coalescing operator?

Is it possible to override the null-coalescing operator for a class in C#? Say for example I want to return a default value if an instance is null and return the instance if it's not. The code would look like something like this: return instance ?? new MyClass("Default"); But what if I would like to use the null-coalescing opera...

Are implicit operators and TypeConverters equivalent?

It seems to me its very easy to implement an implicit operator versus a TypeConverter, so I'm assuming they aren't equivalent because of the prevalence of TypeConverters in the framework (see anything that extends FrameworkElement). But why? Wouldn't it have been much easier to create string->object and object->string implicit operator...

Why C# compiler doesn't call implicit cast operator?

Suppose we have following type: struct MyNullable<T> where T : struct { T Value; public bool HasValue; public MyNullable(T value) { this.Value = value; this.HasValue = true; } public static implicit operator T(MyNullable<T> value) { return value.HasValue ? value.Value : default(T); ...

Overriding an operator using const for both parameters in C++

Hi, I'm trying to create an overridden operator function using both const parameters, but I can't figure out how to do it. Here is a simple example: class Number { Number() { value = 1; }; inline Number operator + (const Number& n) { Number result; result.value = value + n.value; re...