operator-overloading

Linq and the Equality Operator: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object'

I'm trying to override the equality (==) operator in C# to handle comparing any type to a custom type (the custom type is really a wrapper/box around null). So I have this: internal sealed class Nothing { public override bool Equals(object obj) { if (obj == null || obj is Nothing) return true; else return false; } public...

.NET: Where is the default implementation for "==" op_Equality() for value types?

I was poking around in .NET Reflector, and noticed that for reference types like "String" for example, there is an explicit overload of the "==" operator: typeof(string).GetMethod("op_Equality", BindingFlags.Static | BindingFlags.Public) returns: System.Reflection.MethodInfo for the "==" operator. Due to its implementation, you can't...

What does "operator = must be a non-static member" mean? (C++)

I'm in the process of creating a double-linked list, and have overloaded the operator= to make on list equal another: template<class T> void operator=(const list<T>& lst) { clear(); copy(lst); return; } but I get this error when I try to compile: container_def.h(74) : error C2801: 'operator =' must be a non-static member ...

How to overload the indirection operator? (C++)

I'm trying to create an iterator class as a member-class for a list class, and am trying to overload the indirection operator (*) to access the list it's pointing to: template<class T> T list<T>::iterator::operator*(iterator& iter) { return ((iter.lstptr)->current)->data; } where lstptr is a pointer to a list, current is a pointer...

How to differentiate (when overloading) between prefix and postfix forms of operator++? (C++)

Becuase I've overloaded the operator++ for an iterator class template typename list::iterator& list::iterator::operator++() { //stuff } But when I try to do list::iterator IT; IT++; I get a warning about there being no postifx ++, using prefix form. How can I specifically overload the prefix/postifx forms? ...

Custom types as key for a map - C++

I am trying to assign a custom type as a key for std::map. Here is the type which I am using as key. struct Foo { Foo(std::string s) : foo_value(s){} bool operator<(const Foo& foo1) { return foo_value < foo1.foo_value; } bool operator>(const Foo& foo1) { return foo_value > foo1.foo_value; } std::string foo_value; };...

Question about the assignment operator in C++

Forgive what might seem to some to be a very simple question, but I have this use case in mind: struct fraction { fraction( size_t num, size_t denom ) : numerator( num ), denominator( denom ) {}; size_t numerator; size_t denominator; }; What I would like to do is use statements like: fraction f(3,5); ... doub...

How do I write an overload operator where both arguments are interface

I'm using interface for most of my stuff. I can't find a way to create an overload operator + that would allow me to perform an addition on any objects implementing the IPoint interface Code interface IPoint { double X { get; set; } double Y { get; set; } } class Point : IPoint { double X { get; set; } double Y { ...

How can I make a class in python support __getitem__, but not allow iteration?

I want to define a class that supports __getitem__, but does not allow iteration. for example: class b: def __getitem__(self, k): return k cb = b() for x in cb: print x What could I add to the class b to force the "for x in cb:" to fail? ...

Is this the correct way to overload the left-stream operator? (C++)

This function declaration gives me errors: ostream& operator<<(ostream& os, hand& obj); The errors are: error C2143: syntax error : missing ';' before '&' error C4430: missing type specifier error C2065: 'os' : undeclared identifier error C2065: 'obj' : undeclared identifier error C2275: 'hand' : illegal use of this type as an expres...

Do I need to provide cast methods for cast operator overloads in C#, to allow people to use in VB

I read somewhere that older VB .net (pre VB .NET 2005?) couldn't use overloaded operators in a C# class. Is it true for overloaded cast operators? Either way do you guys think it's a disadvantage for a C# class library not to include them and arithmetic methods as actual methods for other languages, like: ToDouble ToFloat FromDouble Ad...

Can you overload the assignment operator for Delphi records?

I have a record that contains a dynamic array. It is normal that when you assign one array variable to another, in fact only the pointer to that array is assigned. This means that when you do that, both the variables point to the same array until you change the size of one of them. Therefore, when I want to assign a separate copy of an a...

Can smart pointers selectively hide or re-direct function calls to the objects they are wrapping?

I'm working on a project where certain objects are referenced counted -- it's a very similar setup to COM. Anyway, our project does have smart pointers that alleviate the need to explicitly call Add() and Release() for these objects. The problem is that sometimes, developers are still calling Release() with the smart pointer. What I'm...

c++ overloading operators, assignment, deep-copy and addition.

I'm doing some exploration of operator-overloading at the moment whilst re-reading some of my old University text-books and I think I'm mis-understanding something, so hopefully this will be some nice easy reputation for some answerers. If this is a duplicate please point me in the right direction. I've created a simple counter class th...

Binary operator overloading on a templated class (C++)

Hi all, I was recently trying to gauge my operator overloading/template abilities and as a small test, created the Container class below. While this code compiles fine and works correctly under MSVC 2008 (displays 11), both MinGW/GCC and Comeau choke on the operator+ overload. As I trust them more than MSVC, I'm trying to figure out what...

F# and op_GreaterThan

I've recently tried to write something in F# making use of Microsoft Solver Foundation Services and while doing so I bumped into an issue: I had to express a (Term > Term) condition which in C# could simply be expressed as t1 > t2 and returns another Term object. In F#, instead, I had to call Term.op_GreaterThan in order to achieve the s...

F# and Operator Overloads

Ok, so can someone explain to me why F# allows you to overload the > and ^ operators, but doesn't allow you to use them? + (op_Addition): Works just fine. ^ (op_Concatenate): Compiler error in F#. Apparently only strings can be concatenated. > (op_GreaterThan): Runtime Error – Failure during generic comparison: the type Program+OppTest4...

Can operators be used as functions? (C++)

This is similar to another question I've asked, but, I've created an expression class that works like so: expression<int, int> exp(10, 11, GreaterThan); //expression<typename T, typename U> exp(T val1, U val2, oper op); //where oper is a pointer to bool function(T, U) where GreaterThan is a previously defined function. And I am wonde...

How do I override < and > in C++/CLI?

I'm porting a class which implements IEquatable<T> and IComparable<T> and overrides ==, !=, < and > from C# into C++/CLI. So far I have: Header: virtual bool Equals(Thing other); virtual int CompareTo(Thing other); static bool operator == (Thing tc1, Thing tc2); static bool operator != (Thing tc1, Thing tc2); static bool operator > (...

C++ shift operator precedence weirdness

Consider the following code: typedef vector<int> intVec; intVec& operator<<(intVec& dst, const int i) { dst.push_back(i); return dst; } int intResult0() { return 23; } int intResult1() { return 42; } // main intVec v; v << intResult0() << intResult1(); The weird thing is, that the compiler generates code, which evalu...