operator-overloading

What are the default return values for operator< and operator[] in C++ (Visual Studio 6)?

I've inherited a large Visual Studio 6 C++ project that needs to be translated for VS2005. Some of the classes defined operator< and operator[], but don't specify return types in the declarations. VS6 allows this, but not VS2005. I am aware that the C standard specifies that the default return type for normal functions is int, and I ass...

Why/When in Python does `x==y` call `y.__eq__(x)`?

The Python docs clearly state that x==y calls x.__eq__(y). However it seems that under many circumstances, the opposite is true. Where is it documented when or why this happens, and how can I work out for sure whether my object's __cmp__ or __eq__ methods are going to get called. Edit: Just to clarify, I know that __eq__ is called in ...

pClass1 = (Class1*)pBase->next without (Class1*) cast

Hi, i have class Base { Base* next; } class Class1 : Base { } Base* pBase = new Base(); Class1* pTest = new Class1(); pBase->next = pTest; Class1* pClass1; pClass1 = (Class1*)pBase->next; I want to be able to write pClass1 = pBase->next; and get no compilation error C2440 (cannot convert). Or in other words I want pClass1 poi...

Assignment vs Initialization in C++

I thought that constructors control initialization and operator= functions control assignment in C++. So why does this code work? #include <iostream> #include <cmath> using namespace std; class Deg { public: Deg() {} Deg(int a) : d(a) {} void operator()(double a) { cout << pow(a,d...

Define new operators in C#?

Possible Duplicate: Is it possible to create a new operator in c#? I love C#, but one thing I wish it had was the ability to define my own operators on classes, like A => B instead of having to do A.Implies(B). I think it would be really cool if you could assign an identifier of any length in a set like [+-*/&^|%$#@><]+ to a c...

C++ Linker Error involving operator overload function

I have a List of type Node. I want to set a temporary Node equal to the Node at the front of the List but I keep getting a Linker Error: class Node { public: Node(); Node& operator = (const Node& n); }; 1>Linking... 1>main.obj : error LNK2019: unresolved external symbol "public: class Node & __thiscall Node::operator=(...

Operator Overloading working but giving stack overflow and crashing in C++

I wrote this Node class and = operator overload function and this is the only way I could get it to compile and run but it just overflows and bomb my program. Can someone please fix it so it works. I don't have a lot of experience with overloading operator in C++. I just want to set a Node object equal to another Node object. Thanks in a...

Overload Add operator for three extended records

Delphi 2006 introduced operator overloading which was then bugfixed in Delphi 2007. This is about Delphi 2007. Why does the following not compile: type TFirstRec = record // some stuff end; type TSecondRec = record // some stuff end; type TThirdRec = record // some stuff class operator Add(_a: TFirstRec; _b:...

+ operator for String in Java

I saw this question a few minutes ago, and decided to take a look in the java String class to check if there was some overloading for the + operator. I couldn't find anything, but I know I can do this String ab = "ab"; String cd = "cd"; String both = ab + cd; //both = "abcd" Where's that implemented? ...

Why doesn't my overloaded comma operator get called?

Hi, I'm trying to overload the comma operator with a non-friend non-member function like this: #include <iostream> using std::cout; using std::endl; class comma_op { int val; public: void operator,(const float &rhs) { cout << this->val << ", " << rhs << endl; } }; void operator,(const float &lhs, const comma_o...

return value of operator overloading in C++

Hi I have a question about the return value of operator overloading in C++. Generally, I found two cases, one is return-by-value, and one is return-by-reference. So what's the underneath rule of that? Especially at the case when you can use the operator continuously, such as cout<<x<<y. For example, when implementing a + operation "str...

How can overloading operator "function call" in C++ be useful?

I recently discovered that in C++ you can overload the "function call" operator, in a strange way in which you have to write two pair of parenthesis to do so: class A { private: int n; public: void operator ()() const; }; And then use it this way: A a; a() ; In what case can this be consider useful ? ...

What's the right way to overload the stream operators << >> for my class?

I'm a bit confused about how to overload the stream operators for my class in C++, since it seems they are functions on the stream classes, not on my class. What's the normal way to do this? At the moment, for the "get from" operator, I have a definition istream& operator>>(istream& is, Thing& thing) { // etc... which works. It's n...

Overloading Operator + in C++

Ok, I am working through a book and trying to learn C++ operator overloading. I created a BigInt class that takes a single int (initially set to 0) for the constructor. I overloaded the += method and it works just fine in the following code: BigInt x = BigInt(2); x += x; x.print( cout ); The code will output 4. So, then I was working ...

why C# not allow operator overloading?

why c# not allowed operator overloading while C++ do? I got this error, when trying to overload. Overloadable binary operator expected My sample code looks like, public static MyClass operator +=(MyClass obj1, MyClass obj2) { ... } ...

Overloading << operator and recursion

Hi, I tried the following code: #include <iostream> using std::cout; using std::ostream; class X { public: friend ostream& operator<<(ostream &os, const X& obj) { cout << "hehe"; // comment this and infinite loop is gone return (os << obj); } }; int main() { X x; cout << x; return 0; }...

operator new overloading and alignment

I'm overloading operator new, but I recently hit a problem with alignment. Basically, I have a class IBase which provides operator new and delete in all required variants. All classes derive from IBase and hence also use the custom allocators. The problem I'm facing now is that I have a child Foo which has to be 16-byte aligned, while a...

C++ wrapper with overloaded = operator

I'm trying to develop a pretty simple (for now) wrapper class around int, and was hoping to overload the = operator to achieve something like the following: class IntWrapper { ... private: int val; } int main ( ) { IntWrapper a; int b; a = 5; // uses overloaded = to implement setter b = a; // uses overl...

what errors will report if I overload operators incorrectly?

I know that we can't overload operator with other meaning, we can't create new operators, and we can't overload without user-defined class. If I overload operators incorrectly? what errors will report? compiler errors or runtime error? If I overload **, what would happen? ...

error C2593: 'operator +' is ambiguous

Hello, If I have the following files, I get this error (c2593 in VC9). If I un-comment the prototype in main.cpp, the error disappears. I need to maintain the same functionality while keeping the class out of main.cpp. How can I do that? Thanks. main.cpp: #include "number.h" //const Number operator + (const Number & lhs, const Numbe...