operator-overloading

Is there a way to overload += in python?

I know about the __add__ method to override plus, but when I use that to override +=, I end up with one of two problems: (1) if __add__ mutates self, then z = x + y will mutate x when I don't really want x to be mutated there. (2) if __add__ returns a new object, then tmp = z z += x z += y tmp += w return z will return something...

Operator Overloading with Interface-Based Programming in C#

Background I am using interface-based programming on a current project and have run into a problem when overloading operators (specifically the Equality and Inequality operators). Assumptions I'm using C# 3.0, .NET 3.5 and Visual Studio 2008 UPDATE - The Following Assumption was False! Requiring all comparisons to use Equals ra...

does assignment operator work with different types of objects?

class A { public: void operator=(const B &in); private: int a; }; class B { private: int c; } sorry. there happened an error. is assignment operator valid ? or is there any way to achieve this? [There is no relation between A and B class.] void A::operator=(const B& in) { a = in.c; } Thanks a lot. ...

Operator = Overload with Const Variable in C++

Hi everybody. I was wondering if you guys could help me. Here are my .h: Class Doctor { const string name; public: Doctor(); Doctor(string name); Doctor & Doctor::operator=(const Doctor &doc); } and my main: int main(){ Doctor d1 = Doctor("peter"); Doctor d2 = Doctor(); d2 = d1; } I want to ...

Arithmetic operator overloading for a generic class in C#

Given a generic class definition like public class ConstrainedNumber<T>: IEquatable<ConstrainedNumber<T>>, IEquatable<T>, IComparable<ConstrainedNumber<T>>, IComparable<T>, IComparable where T:struct, IComparable, IComparable<T>, IEquatable<T>, how can I define arithmetic operators for it? The following does not compile, because the '+'...

Why can't I define both implicit and explicit operators?

Why I cannot define both implicit and explicit operators like so? public class C { public static implicit operator string(C c) { return "implicit"; } public static explicit operator string(C c) { return "explicit"; } } You can do this hack though :) ...

C/C++ replacement/redefinition rules?

I am not particularly new to C/C++ but today I discovered some things that I didn't expect. This compiles in gcc: /* test.c */ #include <stddef.h> // ! typedef unsigned long int size_t; // NO ERROR typedef unsigned long int size_t; // NO ERROR int main(void) { typedef unsigned long int size_t; // NO ERROR return 0; } This doesn...

Is it possible to overload ++ operators in Python?

Is it possible to overload ++ operators in Python? ...

What's the best way to implement a operator overload?

Among all things I've learned in C++ (which isn't so much), operator overloading seems the most difficult. In general terms, when is it best to write an operator overload as a friend function? When do I have to explicilty use *this? Is always bad to use a temporary object? ...

overload operator = in C#. How can i accept other types?

So a friend was telling me how a game was hacked and how the technique worked. He then asked whats the best way to prevent that kind of attack. The most straight forward way i knew was to A) the shuffle the bits of important value B) hash the values and compare them every time (an int that holds the score or money is likely to be checked...

How can i use generic in an implicit operator?

I have a c++ class that is very simple struct Pt_t { T x, y; template <class T2> operator Pt_t<T2>() { Pt_t<T2> pt = {x, y}; return pt; } }; That allows me to create a pt that has T as any type i want. I can also do Pt_t<s8> = Pt_t<u64>; without a problem. How do i do the same in C#? i tried the below and got an error cla...

How do I overload operator for a generic class?

It seems to me that it is not only my problem. Please do not close my question as a duplicate because I looked these questions through and I did not find the solution. class Matrix<T> { private Int32 rows; private Int32 cols; private T[,] matrix; public Matrix() { rows = cols = 0; ...

Operator Overloading in PHP

Is it possible to overload operators in PHP? Specifically I would like to create an Array class and would like to overload the [] operator. ...

Making a Point class in c++

Right now I am using std::pair to represent a 2d point in c++. However, I am getting annoyed with having to write typedef std::pair<double, double> Point; Point difference = Point(p2.first - p1.first, p2.second - p1.second); instead of being able to overload operator+ and operator-. So, my question is, to ma...

DateTime difference operator considers daylight saving?

As far as I know the difference operator of the DateTime type considers leap years: so new DateTime(2008, 3, 1) - new DateTime(2008, 2, 1) // should return 29 days new DateTime(2009, 3, 1) - new DateTime(2009, 2, 1) // should return 28 days But what about daylight saving? ...

Why is the Visual C++ compiler calling the wrong overload here?

Why is the Visual C++ compiler calling the wrong overload here? I am have a subclass of ostream that I use to define a buffer for formatting. Sometimes I want to create a temporary and immediately insert a string into it with the usual << operator like this: M2Stream() << "the string"; Unfortunately, the program calls the operator<<(...

Have a C++ Class act like a custom ostream, sstream

I have a C++ class MyObject and I want to be able to feed this data like I would to a osstream (but unlike a direct sstream, have the incoming data be formatted a special way). I can't seem to figure out how to overload a operator for MyObject to eat input given to it. class MyObject { public: ostringstream s; FEEDME }; int m...

Operator overloading >>

Hey, I'm very new to C++ operator overloading and having some teething trouble. I've defined: 'void Graph::operator>>(const char* param)' to accept a string and then input then perform certain actions on the object. How do I call this function that I've defined (>>) ? In what ways can I use it? ...

Implicit operator?

I need some help. I am creating a SelectItem class like this: public class SelectItem<T> where T : class { public bool IsChecked { get; set; } public T Item { get; set; } } I would like the following code to be valid SelectItem<String> obj = new SelectItem<String> { Item = "Value" }; obj.IsChecked = true; String objValue = ...

c++ Operator overloading "-"

I've done operator overloading to "-" for my class graph. It's use isn't totally intuitive (bad coding-I know) but if I do graph3 = graph2-graph1 then graph 3 is supposed to receive only those vertexes in both graph 2 and graph 1. So, I've written the code and when I run the debugger, the operator- function seems to create a new "grap...