operator-overloading

Compiler error when using nested operator overloading in C++

I have a URL class that overloads the ==, <, >, and != operators for simple comparison. The URL class has a string data member and some functions to act on the string. The operators work fine when tested with the URL class. I also have a Page class that has a URL data member. I am trying to overload the same operators in the Page class...

F# dynamic lookup operator (?) overloading

Can't define (?) operator overload on type: type Foo = val s : string new(s) = { s = s } static member (?) (foo : Foo, name : string) = foo.s + name let foo = Foo("hello, ") let hw = foo? world // error FS0043: The member or object constructor 'op_Dynamic' // takes 2 argument(s) but is here given 1. The required signat...

printing using one '\n'

I am pretty sure all of you are familiar with the concept of the Big4, and I have several stuffs to do print in each of the constructor, assignment, destructor, and copy constructor. The restriction is this: I CAN'T use more than one newline (e.g., ƒn or std::endl) in any method I can have a method called print, so I am guessing print...

Ambiguous overload on template operators

I am working on two wrapper classes that define real and complex data types. Each class defines overloaded constructors, as well as the four arithmetic operators +,-,*,/ and five assignment operators =,+= etc. In order to avoid repeating code, I was thinking of using template functions when the left- and right-hand-side arguments of an o...

Overloading Arithmetic Operators in JavaScript?

This is the best way I can think of phrasing this question, given this JavaScript "class" definition: var Quota = function(hours, minutes, seconds){ if (arguments.length === 3) { this.hours = hours; this.minutes = minutes; this.seconds = seconds; this.totalMilliseconds = Math.floor((hours * 3600000))...

Should I Overload == Operator?

How does the == operator really function in C#? If it used to compare objects of class A, will it try to match all of A's properties, or will it look for pointers to the same memory location (or maybe something else)? Let's create a hypothetical example. I'm writing an application that utilizes the Twitter API, and it has a Tweet class,...

Why does not C# support operator overloading with pass by reference?

Is this a CLR restriction or a language design decision? I tried to do it in C++/CLI, of course where it works because the need to support native c++: public ref class Test { public: static Test^ operator &( Test^ msg, int& i ) { i = i + 1; return nullptr; } }; and then looked at the compiler ...

Is this a good/efficient idiom for implementing Equals and equality/inequality operators?

I have had a few problems getting this right, so I wanted to ask if anyone has any feedback on whether this is an efficient way to implement the Equals method and equality/inequality operators for a custom immutable class. These operators are called very frequently by my program, so I want to make sure I get them right. class MyObj { ...

Overriding instance variable array's operators in Ruby

Sorry for the poor title, I don't really know what to call this. I have something like this in Ruby: class Test def initialize @my_array = [] end attr_accessor :my_array end test = Test.new test.my_array << "Hello, World!" For the @my_array instance variable, I want to override the << operator so that I can first process wh...

In the msdn guidance on Equals override, why the cast to object in the null check?

I was just looking at the Guidelines for Overloading Equals() on msdn (see code below); most of it is clear to me, but there is one line I don't get. if ((System.Object)p == null) or, in the second override if ((object)p == null) Why not simply if (p == null) What is the cast to object buying us? public override bool Equals(Sy...

C++ operator[] syntax.

Just a quick syntax question. I'm writing a map class (for school). If I define the following operator overload: template<typename Key, typename Val> class Map {... Val* operator[](Key k); What happens when a user writes: Map<int,int> myMap; map[10] = 3; Doing something like that will only overwrite a temporary copy of the [null]...

C# in operator-overloading

Hi, I just had an idea last nigth when writing an if-expression and sometimes the expression tend to be long when you have it like this: if(x == 1 || x == 2 || x == 33 || x == 4 || x == -5 || x == 61) { ... } x can be enums,strings,ints,chars you get the picture. I want to know if there are an easier way of writing this. I think of ...

Overloading operator>> to a char buffer in C++ - can I tell the stream length?

I'm on a custom C++ crash course. I've known the basics for many years, but I'm currently trying to refresh my memory and learn more. To that end, as my second task (after writing a stack class based on linked lists), I'm writing my own string class. It's gone pretty smoothly until now; I want to overload operator>> that I can do stuff ...

Operator overloading in Java

Please can you tell me if it is possible to overload operators in Java? If it is used anywhere in Java could you please tell me about it. ...

Overloading the + operator to add two arrays

What's wrong with this C# code? I tried to overload the + operator to add two arrays, but got an error message as follows: One of the parameters of a binary operator must be the containing type. class Program { public static void Main(string[] args) { const int n = 5; int[] a = new int[n] { 1, 2, 3, 4, 5 }; int[]...

What's the right way to overload operator== for a class hierarchy?

Suppose I have the following class hierarchy: class A { int foo; virtual ~A() = 0; }; A::~A() {} class B : public A { int bar; }; class C : public A { int baz; }; What's the right way to overload operator== for these classes? If I make them all free functions, then B and C can't leverage A's version without casting...

Can't get operator overloading to work with Linq Expression Trees

I am creating Linq expression trees from F# that operates on a custom datatype I have. The type is a very simple discriminated union that has the usual arithmetic operators overloaded. But for some reason I cannot create arithmetic linq expression nodes due to the fact that it can't find the correct overload. Thing is, I swear I had this...

Overloading operator >

Hello, In my homework, I have to design a class Message; among other attributes, it has attribute "priority" (main goal is to implement priority queue). As in container I must check if one object is greater than other, I have overloaded operator '>'. Now, I have a few general questions about it... Question one: If I overload operato...

How to convert C++ class/struct to a primitive/different type/class/struct?

Hi all! I have the following class CppProperty class that holds value: template<typename TT> class CppProperty { TT val; public: CppProperty(void) { } CppProperty(TT aval) : val(aval) { } CppProperty(const CppProperty & rhs) { this->val = rhs.val; } virtual ~CppProperty(void) { ...

Overloading *(iterator + n) and *(n + iterator) in a C++ iterator class?

(Note: I'm writing this project for learning only; comments about it being redundant are... uh, redundant. ;) I'm trying to implement a random access iterator, but I've found very little literature on the subject, so I'm going by trial and error combined with Wikpedias list of operator overload prototypes. It's worked well enough so far...