operator-overloading

Overloading the "Set to equal to" operator.

I was reading a Business Primitives by CodeBetter.com and was toying around with the idea. Taking his example of Money, how would one implement this in a way that it can be used similarily as regular value types? What I mean by that is do this: Money myMoney = 100.00m; Instead of: Money myMoney = new Money(100.00m); I understand ...

Tribool implementation in C#

I'm trying to implement a Tribool type using http://www.boost.org/doc/libs/1_41_0/doc/html/tribool.html as reference. I'm using a struct since it's a primitive type and shouldn't be extendable. I know there are 3 types of Tribools---True, False, and Unknown, and the default constructor should provide a False Tribool. My question is, wh...

Template class and class properties that are also templates

I'm having trouble compiling the following header. It's my first experience with templates and I guess I'm getting something wrong. The compilers point errors at vector<vector<T>> data_; and the operator overloading function. I would like the data_ vector to have the same type as the OptBaseMatrix object but I'm not sure how to do it... ...

C# Overloading Operator == and !=

I am having problems getting the desired behavior out of these few classes and interfaces. Here is my problem, //Inside a Unit Test that has access to internal methods and properties INode firstNode, secondNode; INodeId id = new NodeId (4); first = new Node (id, "node"); second = new Node (id, "node"); Assert.IsTrue (first == sec...

C++ Operator overloading example

Hi! Well, I'm new to operator overloading, and I found this problem. Instead of documenting myself, I prefer to ask you :D The point is, I know how to do simple operator overloading, but I'm facing problems with stacking operators. I'll try to put a relatively simple example: struct dxfdat { int a; string b; /* here is the question...

Should i override operators new/delete in my c++ WIn32 applications

Hi, I know Microsoft themselves used to recommend overriding operator new with calls to HeapCreate() and HeapAlloc(), but that was a while ago. See KB139638 for more details. Would i still benefit from overriding new/delete on Win32? What would be the recommended implementation? TIA. ...

How to use C++ operators within python using boost::python (pyopencv)

I'm using the pyopencv bindings. This python lib uses boost::python to connect to OopenCV. Now I'm trying to use the SURF class but don't know how to handle the class operator in my python code. The C++ class is defined as: void SURF::operator()(const Mat& img, const Mat& mask, vector<KeyPoint>& keypoints) const {...}...

operator overloading c++

When overloading operators, is it necessary to overload >= <= and !=? It seems like it would be smart for c++ to call !operator= for !=, !> for operator<= and !< for operator>=. Is that the case, or is it necessary to overload every function? ...

Override 'in' operator in Python

If I am creating my own class in Python, what function should I define so as to allow the use of the 'in' operator, e.g. class MyClass(object): ... m = MyClass() if 54 in m: ... ...

c++ operator[] overloading problem (works fine but not for pointers, why?)

Problem with operator[] in c++, i have some class: 197 class Permutation{ 198 private: 199 unsigned int* array; 200 unsigned int size; 201 202 void fill(){ 203 for(unsigned int i=0;i<size;i++) 204 array[i]=i; 205 ...

Break out of an overloaded extraction operator? (C++)

I'm trying to use an overloaded ">>" to scan input from a file. The problem is, I have no idea how to deal with end of file. In this case, my file is composed of a number, followed by several chars Ex: 9rl 8d 6ff istream &operator>>(istream &is, Move &move) { char c; int i = 0; c = is.get(); if (!isalnum(c)) return...

Overloading the global type conversion operator

To test and display the result of some functions of my library, I am creating a set of handy functions. I have an execute function that looks like : template <typename R, typename I> std::string execute( const std::string& func_name, R(*func_ptr)( const I& ), const I& func_input ); It calls the function, and display the results and a...

how do I write a logger class with cout style interface (logger << "Error: " << val << endl;)

I want to create a logger class such that with a functionality like this: Logger log; log << "Error: " << value << "seen" << endl; This should print me a custom formatted message. E.g. "12-09-2009 11:22:33 Error 5 seen" My simple class currently looks like this: class Logger { private: ostringstream oss; public: ...

trying to make a simple grid-class, non-lvalue in assignment

I'm implementing a simple C++ grid class. One Function it should support is accessed through round brackets, so that I can access the elements by writing mygrid(0,0). I overloaded the () operator and i am getting the error message: "non-lvalue in assignment". what I want to be able to do: //main cGrid<cA*> grid(5, 5); grid(0,0) = new...

Order of operator overload resolution involving temporaries

Consider the following minimal example: #include <iostream> using namespace std; class myostream : public ostream { public: myostream(ostream const &other) : ostream(other.rdbuf()) { } }; int main() { cout << "hello world" << endl; myostream s(cout); s << "hello world" << endl; myostr...

Why can't operators be overloaded inside a static class?

I have an extension class for System.Net.IPAddress and I was wanting to overload the binary operators >, <, == but the compiler is telling me that I can't overload those operators inside a static class, which I must have for my other extension methods. Is there a particular reason for this? Thanks. ...

How does operator overloading (especially 'new') arity work?

I've never quite understood how the argument lists for operator overloading are determined in a systematic way, and I'm particularly confused by a problem I have now. When you overload a unary operator it has one argument, or zero if it's a class member. When you overload a binary operator it has two arguments, or one if it's a class m...

bool operator() and inheritance

I have the following problem with bool operator() for derived class Base class class Point { double x, y; public: Point(){x=0;y=0;} ... } Derived class class 3DPoint : public Point { double z; public: 3DPoint(double x, double y, double zx) : Point(x,y){z(zz);} ... } operator () for derived class class compareByX { bool operator (...

overloading augmented arithmetic assignments in python

Hi all. I'm new to Python so apologies in advance if this is a stupid question. For an assignment I need to overload augmented arithmetic assignments(+=, -=, /=, =, *=, %=) for a class myInt. I checked the Python documentation and this is what I came up with: def __iadd__(self, other): if isinstance(other, myInt): self.a +...

Why is the delete operator required to be static?

I found this one question asking the same thing, however only the 'new' part was answered, so here goes again. Why is the delete operator required to be static? Somehow it doesn't make sense. The new operator makes perfect sense, just like the constructor can't be virtual, neither can the new operator. However, the destructor can (and s...