operator-overloading

Hard to find bug on a custom C++ class

I need help on finding the problem using a custom c++ class to manage 3D positions. Here is the relevant code from the class Punto operator+(Punto p){ return Punto(this->x + p.x, this->y + p.y, this->z + p.z); } Punto operator+(Punto *p){ return Punto(this->x + p->x, this->y + p->y, this->z + p->z); } Punto operator-...

Is it possible in C# to overload a generic cast operator in the following way?

Hi, Just wondering if there is anyway to represent the following code in C# 3.5: public struct Foo<T> { public Foo(T item) { this.Item = item; } public T Item { get; set; } public static explicit operator Foo<U> ( Foo<T> a ) where U : T { return new Foo<U>((U)a.Item) } } Thanks ...

When implementing operator[] how should I include bounds checking?

First of all I apologize for the long lead up to such a simplistic question. I am implementing a class which serves as a very long 1 dimensional index on a space filling curve or the n-tuple representing the Cartesian coordinate that index corresponds to. class curvePoint { public: friend class curveCalculate; //Construction a...

(C#) Problems when overloading the == operator

Hello. I overloaded the == operator on my class as follows: public static bool operator ==(Table pt1, Table pt2) { return Compare(pt1, pt2) == 0 && pt1.TableName == pt2.TableName; } Compare will work just as the strcmp does in c++, returning an integer. Problem is that if I do an if (MY_CLASS == null), it will call my == operator...

Defining a proper subtraction operator

I wrote an abstraction class for a math object, and defined all of the operators. While using it, I came across: Fixed f1 = 5.0f - f3; I have only two subtraction operators defined: inline const Fixed operator - () const; inline const Fixed operator - (float f) const; I get what is wrong here - addition is swappable (1 + 2 == 2 + ...

__lt__ instead of __cmp__

Python 2.x has two ways to overload comparison operators, __cmp__ or the "rich comparison operators" such as __lt__. The rich comparison overloads are said to be preferred, but why is this so? Rich comparison operators are simpler to implement each, but you must implement several of them with nearly identical logic. However, if you ca...

overloading [][] in c#

I'm building a tree-based data structure and overloaded [ ] so that I can say node["key1", "key2", "key3"] which returns the node whose parents 1, 2, and 3 levels above are the nodes with those keys. the nodes conceptually map to an array of data, so what I have now is this function: node[keys...].SetValue(i, value) which sets the ...

C#: How to make objects "assume" they got overload for operator X?

Hi. Is there a way of forcing the C# compiler to ignore missing operator overloads for objects, and instead handle that check in runtime? I ask because I have a container that has multiple objects that has various attributes of type int, string, ushort and so on. I'm making a search function for that container, and would like to be able...

C++ operators and arguments

Hi! Let's say I have a class Point: class Point { int x, y; public: Point& operator+=(const Point &p) { x=p.x; y=p.y; return *this; } }; Why can I not call this as such: Point p1; p1 += Point(10,10); And is there any way to do this, while still having a reference as the argument? ...

Overloading += in c++

If I've overloaded operator+ and operator= do I still need to overload operator+= for something like this to work: -?- MyClass mc1, mc2; mc1 += mc2; ...

What makes Scala's operator overloading "good", but C++'s "bad"?

Operator overloading in C++ is considered by many to be A Bad Thing(tm), and a mistake not to be repeated in newer languages. Certainly, it was one feature specifically dropped when designing Java. Now that I've started reading up on Scala, I find that it has what looks very much like operator overloading (although technically it doesn'...

Function template with an operator (C++)

In C++, can you have a templated operator on a class? Like so: class MyClass { public: template<class T> T operator()() { /* return some T */ }; } This actually seems to compile just fine, but the confusion comes in how one would use it: MyClass c; int i = c<int>(); // This doesn't work int i = (int)c(); // Neither does this*...

Why does operator overloading work by return type while it is not allowed for methods?

C++ does not allow polymorphism for methods based on their return type. However, when overloading an operator this seems possible. Does anyone know why? I thought operators are handled like methods internally. Edit: Here's an example: struct func { operator string() { return "1";} operator int() { return 2; } }; int main( ) {...

How do I overload the << operator?

I intend to call a function whenever m_logger<<"hello"<<"world" is called. m_logger is of type ofstream. So i decide to overload << with following signature friend ofstream& operator<<(ofstream &stream,char *str); However the vc compiler gives following error: error C2666: 'operator <<' : 6 overloads have similar conversions Is...

Is it possible to assign object to int?

I have a CCounter class which holds and integer value protected by mutex. I've defined several operators like post/pre inc/dec returning an integer so I can do: CCounter c(10); int i = c++; but what do I do with a simple assignment like i = c ? I tried to define friend operator= but it gives me operator=(int&, const CCounter&)’ must ...

Overload the += event operator

Is there a way to overload the event += and -= operators in C#? What I want to do is take an event listener and register it to different events. So something like this: SomeEvent += new Event(EventMethod); Then instead of attaching to SomeEvent, it actually attaches to different events: DifferentEvent += (the listener above); Anot...

Why are some operators in C++ only allowed to be overloaded as member functions?

The operators are = () [] -> ->* conversion operators These can be declared only as member functions. Any other operator function can be either a class member or a non-member function. What is the rationale for this restriction? ...

Understanding javascript bitwise NOT

Thanks to everyone in advance - alert((~1).toString(2)); outputs: -10 but in PHP/Java it outputs 11111111111111111111111111111110 Am I missing something, why does Javascript add a "-" to the output? Thx, Sam ...

how does ofstream or ostream type cast all types to string?

any system defined user type past to ostream object is converted to a string or char* ? like cout<<4<<"Hello World"; works perfectly fine, how is this achieved? is the << operator overloaded for each and every type? is there a way to achieve it through just one generic overloaded function? what i mean is can i have just one overloaded...

std::endl is of unknown type when overloading operator<<

I overloaded operator << template <Typename T> UIStream& operator<<(const T); UIStream my_stream; my_stream << 10 << " heads"; Works but: my_stream << endl; Gives compilation error: error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'UIStream' (or there is no acceptable conversion) What is ...