operator-overloading

Sorting only using the less-than operator compared to a trivalue compare function

In C++/STL sorting is done by using only the less-than operator. Altough I have no idea how the sorting algorithms are actually implemented, I assume that the other operations are created implicite: a > b *equals* b < a == true a == b *equals* !(a < b) && !(b < a) Compared to using a trivalue* compare function, like for example Java, ...

Copy constructor and dynamic allocation

I would like to ask you how to write a copy constructor (and operator = ) for the following classes. Class Node stores coordinates x,y of each node and pointer to another node. class Node { private: double x, y; Node *n; public: Node (double xx, double yy, Node *nn) : x(xx), y(yy), n(nn) {} void setNode (Node *nn) : n(nn) {} ... }; ...

Can I overload operators on enum types in C++?

For example, if I have: typedef enum { year, month, day } field_type; inline foo operator *(field_type t,int x) { return foo(f,x); } inline foo operator -(field_type t) { return t*-1; } int operator /(distance const &d,field_type v) { return d.in(v); } Because if I do not define such operators it is actually legal to write da...

Question on overloading operator+

Consider the following code: class A { public: A const A const A }; int main() { A a; a = ( a + a ) + 5; // error: binary '+' : no operator found which takes a left-hand operand of type 'const A' } Can anyone explain why the above is returned as an error? "( a + a )" calls "const A& operator+( const A& )" and ...

Implementing __concat__ in Python

I tried to implement __concat__, but it didn't work >>> class lHolder(): ... def __init__(self,l): ... self.l=l ... def __concat__(self, l2): ... return self.l+l2 ... def __iter__(self): ... return self.l.__iter__() ... >>> lHolder([1])+[2] Traceback (most recent call last): File "<stdi...

What is an overloaded operator in c++?

I realize this is a basic question but I have searched online, been to cplusplus.com, read through my book, and I can't seem to grasp the concept of overloaded operators. A specific example from cplusplus.com is: // vectors: overloading operators example #include <iostream> using namespace std; class CVector { public: int x,y; ...

Is there a way to add methods to GregorianCalendar in Java?

I'd like to make a method called "isBetween" returning a boolean, seeing if a GregorianCalendar date falls between two others. Alternatively, I'd like to just define operators of < and > for the class. I'm pretty new to Java, so I'm not sure....can I even do either of those? If so, how? ...

Overloading enum operator C# and Silverlight

Just an academical question: Is it possible to avoid int casting when comparing Enum to int? int i = 0; if(i == (int)MyEnum.Whatever) { } I would like to overload == operator in such a manner: public static MyEnum operator ==(int lhs, MyEnum rhs) {} Thanks for reading ;-) ...

C++ operator + and * non-const overloading

I have the following tricky problem: I have implemented a (rather complicated) class which represents mathematical functions in a multiwavelet basis. Since operations like +, - and * are quite natural in this context, I have implemented overloaded operators for this class: FunctionTree<D> operator+(FunctionTree<D> &inpTree); FunctionTre...

Operator overloading outside class

There are two ways to overload operators for a C++ class: Inside class class Vector2 { public: float x, y ; Vector2 operator+( const Vector2 & other ) { Vector2 ans ; ans.x = x + other.x ; ans.y = y + other.y ; return ans ; } } ; Outside class class Vector2 { public: float x, y ; ...

Polynomial division overloading operator (solved)

Ok. here's the operations i successfully code so far thank's to your help: Adittion: polinom operator+(const polinom& P) const { polinom Result; constIter i = poly.begin(), j = P.poly.begin(); while (i != poly.end() && j != P.poly.end()) { //logic while both iterators are valid if (i->pow > j->pow) { //if the cu...

Defining < for STL sort algorithm - operator overload, functor or standalone function?

I have a stl::list containing Widget class objects. They need to be sorted according to two members in the Widget class. For the sorting to work, a less-than comparator comparing two Widget objects must be defined. There seems to be a myriad of ways to do it. From what I can gather, one can either: a. Define a comparison operator overl...

Why friend overloaded operator is preferred to conversion operator in this case

Hi I have a code like this, I think both the friend overloaded operator and conversion operator have the similar function. However, why does the friend overloaded operator is called in this case? What's the rules? Thanks so much! class A{ double i; public: A(int i):i(i) {} operator double () const { cout<<"conversion opera...

What am i doing wrong

I have the following code. I need B class to have a min priority queue of AToTime objects. AToTime have operator>, and yet i receive error telling me than there is no operator> matching the operands... #include <queue> #include <functional> using namespace std; class B{ //public functions public: B(); virtual ~B(); //priva...

Overloading assignment operator in C++

As I've understand, when overloading operator=, the return value should should be a non-const reference. A } It is non-const to allow non-const member functions to be called in cases like: ( a = b ).f(); But why should it return a reference? In what instance will it give a problem if the return value is not declared a reference, ...

Overloading function call operator in C#

Hi, Is it possible to overload the default function operator (the () operator) in C#? If so - how? If not, is there a workaround to create a similar affect? Thanks, Asaf EDIT: I'm trying to give a class a default operator, something along the lines of: class A { A(int myvalue) {/*save value*/} public static int operator() (A...

F# operator over-loading question

The following code fails in 'Evaluate' with: "This expression was expected to have type Complex but here has type double list" Am I breaking some rule on operator over-loading on '(+)'? Things are OK if I change '(+)' to 'Add'. open Microsoft.FSharp.Math /// real power series [kn; ...; k0] => kn*S^n + ... + k0*S^0 type Powe...

Why friend function is preferred to member function for operator<<

When you are going to print an object, a friend operator<< is used. Can we use member function for operator<< ? class A { public: void operator<<(ostream& i) { i<<"Member function";} friend ostream& operator<<(ostream& i, A& a) { i<<"operator<<"; return i;} }; int main () { A a; A b; A c; cout<<a<<b<<c<<endl; a<<cout...

C++ streams operator << and manipulators / formatters

First, most of my recent work was Java. So even though I "know" C++, I do not want to write Java in C++. And C++ templates are one thing I will really miss when going back to Java. Now that this out of the way, if I want to do create a new stream formatter, say pic, that will have a single std::string parameter in it's constructor. I...

Overloading the QDataStream << and >> operators for a user-defined type

I have a an object I'd like to be able to read and write to/from a QDataStream. The header is as follows: class Compound { public: Compound(QString, QPixmap*, Ui::MainWindow*); void saveCurrentInfo(); void restoreSavedInfo(QGraphicsScene*); void setImage(QPixmap*); QString getName(); private: QString name, hom...