operator-overloading

Canonical operator overloading?

Is there a canonical or recommended pattern for implementing arithmetic operator overloading in C++ number-like classes? From the C++ FAQ, we have an exception-safe assignment operator that avoids most problems: class NumberImpl; class Number { NumberImpl *Impl; ... }; Number& Number::operator=(const Number &rhs) { NumberIm...

How do I overload the [] operator in C#

I would like to add an operator to a class. I currently have a GetValue() method that I would like to replace with an [] operator. class A { private List<int> values = new List<int>(); public int GetValue(int index) { return values[index]; } } ...

Quick and dirty operator!=

In my classes I often write a quick operator!= by returning !(*this == rhs), e.g.: class Foo { private: int n_; std::string str_; public: ... bool operator==(const Foo& rhs) const { return n_ == rhs.n_ && str_ == rhs.str_; } bool operator!=(const Foo& rhs) const { return !(*this == rhs); ...

Overload [] operator in AS3

I just finished writing my own collecion class, and i'd really like to make it iterable with the for each or the simple for construct, or just to access elements with the collection[key] notation. I've written a getElementAt(index):MyOwnElement function, but using it, is not as sexy as using the square brachets, don't even let me start ...

Why won't cout << work with overloaded * operator?

I'm creating my first class, mainly guided by Overland's C++ Without Fear. I've made the overloaded friend ostream operator<<, which works fine. I've also overloaded the * operator, and that works fine. What doesn't work is when I try to output the result of the * operator directly: BCD bcd(10); //bcd is initialised to 10 BCD bcd2(15);...

overloaded increment's return value

In his The C++ Programming Language Stroustrup gives the following example for inc/dec overloading: class Ptr_to_T { T* p; T* array ; int size; public: Ptr_to_T(T* p, T* v, int s); // bind to array v of size s, initial value p Ptr_to_T(T* p); // bind to single object, initial value p Ptr_to_T& operator++(); // prefix Ptr_to_T ope...

How to properly overload the << operator for an ostream?

I am writing a small matrix library in C++ for matrix operations. However my compiler complaints, where before it did not. This code was left on a shelf for 6 months and in between I upgraded my computer from debian etch to lenny (g++ (Debian 4.3.2-1.1) 4.3.2 ) however I have the same problem on a Ubuntu system with the same g++. Here i...

VB.NET (Of T) Comparison Operator

In the sample code, the line with the 'error comment' gives the following the error - Operator '<' is not defined for types 'T' and 'T'. Why wouldn't VB automatically call the appropriate T operator? (i.e. If T is an integer then call the integer comparison functions.) Is it possible to make this work in an elegant fashion? This i...

how define friend function and operator overloading in c++

i want to know about the friend function and operator overloading in c++ and how they work? with program? ...

Why can't you overload the '.' operator in C++?

It would be very useful to be able to overload the . operator in C++ and return a reference to an object. You can overload operator-> and operator* but not operator. Is there a technical reason for this? ...

Can I define a Perl-like binding operator (=~) in C#?

I really like being able to use =~ and !~ in Perl to evaluate a string against a regular expression. I'd like to port this functionality over to C#, but it appears that, while you can overload operators, you can't create new ones. I'm considering extending the string type to provide a Match() method that will allow me to pass a regular ...

Overloading . -> and :: for use in multiplatform classes

Say I have three window classes, one for each OS I want to support: WindowsWindow OSXWindow LinuxWindow They all inherit from the Window class. This is also the class you instantiate. The Window class have the . -> and :: operators overloaded, and depending on which OS were running on (based on IFDEFs) it casts the this pointer to ...

Overloading operator<< for primitive types. Is that possible?

Hey. Is it possible to overload operator<< for primitive types? Fx lets say that I want to write a std::endl each time want to write a int. Can I overload operator<< for int, so that it automatic puts a std::endl to the output? I have tried with this, std::ostream& operator<<(std::ostream& strm, int & i) { strm << i << std::endl; ...

Encapsulate a simple type using C++ templates

I'm looking to add functionality to all the simple types in C++. I want to write a single templated class that takes as a template parameter the type to be encapsulated and then has all the operators defined so that the encapsulated class works exactly as the simple type it encapsulates. Something like this: template <typename _Simpl...

Odd behavior with operator>= overloading

Hello, I'm having a strange behavior with an operator overloading in C++. I have a class, and I need to check if its contents are greater or equal to a long double. I overloaded the >= operator to make this check, my declaration is as follows: bool MyClass::operator>=(long double value) const; I have to say that I also have a cast-to-...

Compiler not creating templated ostream << operator

I have a class, defined in a head as: template <typename T> class MyClass { template <typename U> friend std::ostream& operator<<(std::ostream& output, const MyClass<U>& p); public: ... } In an implementation file, I have: template <typename U> std::ostream& operator<<(std::ostream& output, const MyClass<U>& m) { outpu...

c++ specialized overload?

-edit- i am trying to close the question. i solved the problem with boost::is_base_and_derived In my class i want to do two things. 1) Copy int, floats and other normal values 2) Copy structs that supply a special copy function (template T copyAs(); } the struct MUST NOT return int's unless i explicitly say ints. I do not want the progr...

Operator overloading in C++

I have the following constructor in my "fraction class" fraction::fraction(int num, int den=1) { numerator=num; denominator=den; } I also have a function called lcm() that returns the least common multiple of values given as parameters. I need to overload the "+" operator in the class fraction. The operator "+" is to add t...

How can a C# class be written to test against 0 as well as null

BACKGROUND: I have a custom class written in C# (2005), with code similar to the following: public class Savepoint { public int iOffset; /* Starting offset in main journal */ public u32 nOrig; /* Original number of pages in file */ public u32 iSubRec; /* Index of first record in su...

Why can '=' not be overloaded in C#?

I was wondering, why can't I overload '=' in C#? Can I get a better explanation? ...