operator-overloading

Can I override an overload of an operator and return a different type?

class A{ public: virtual char &operator[](int); protected: .. }; class B:A{ public: A* &operator[](int); protected: } Can I change the return type when I overload an overload of an operator? thanks! //EDIT Okay, so now that we established that this wont work how can I build a work around? Lets say I have classe...

Does it ever make sense to overload unary operator &?

So, C++ allows overloading the unary operator &(address). Are you aware of any real-world example when operator & was rightfully overloaded? And a second, more specific question, are you aware of any real-world example when operator & was rightfully overloaded while preserving address semantics? TIA ...

Overloading operator < with const, but don't insert into map as const

I'm having an issue. I have a class with an overloaded operator like this.. class Foo { friend bool operator<(const Foo &a, const Foo &b); ... }; bool operator<(const Foo &a, const Foo &b) { return a.member < b.member; } Then in a function in a class that holds some Foos in a map as keys... void Bar::Update()...

operator overloading

iarray<T>& operator = (iarray<T>& v) Why the return type is iarray<T>& not iarray<T> ? UPDATE Can someone elaborate in great detail why iarray<T> const &v ? ...

Generic Arithmetic in Java

Hello all, I have a filter class wherein the user must declare the type (e.g. Filter<Double>, Filter<Float> etc). The class then implements a moving average filter so objects within the class must be added. My question is how to do this? I'm sorry if the answer is simple but I've muddled myself up by thinking about it too much I think :...

Overloading implicit in c++

full disclosure - this is for a homework assignment. And I normally would not ask for homework help, but here it is. I'm asked to provide 5 examples of "overloading implicit in c++". I'm sure he is referring to operator overloading for types such as char, int, float, etc in iostream and the types themselves. I understand explicitly o...

Get a copy of "this" (current instance) in C++

Hi, I want to have a copy of the currently running instance. When i change a value in the copy, original object is also affected. The copy acts as an instance. How to avoid this? I need to create an independent copy of the calling object. Set operator+(Set s){ Set temp = *this; for(int i=0; s.elements[i...

How can I "override" [] to accept two arguments in C++?

I am trying to create a bit-vector class in C++ to model some hardware. In most HDLs (hardware description langauges) that I know, specific bits are referenced like this: my_vector[bit_position] and sub-vectors are referenced like this: my_vector[msb:lsb] or my_vector[msb,lsb] I want to be able to do something similar with my b...

Lexically-scoped ordering behavior

I have a class with two definitions of ordering. (In the real problem, one is a total order and one is a semiorder.) But it's nice to be able to use the comparison operators rather than always having to use an explicit comparison function or functor object. So I figured I'd provide some comparison operators like this: class C; names...

What do I often see references in operator overloading definitions ?

For example, in the OGRE3D engine, I often see things like class_name class_name :: operator + (class_name & object) Instead of class_name class_name :: operator + (class_name object) Well it's not that I prefer the second form, but is there a particular reason to use a reference in the input ? Does it has special cases where it is...

C++ template friend operator overloading

What is wrong with my code? template<int E, int F> class Float { friend Float<E, F> operator+ (const Float<E, F> &lhs, const Float<E, F> &rhs); }; G++ just keeps warning: float.h:7: warning: friend declaration ‘Float<E, F> operator+(const Float<E, F>&, const Float<E, F>&)’ declares a non-template function float.h:7: warning: (if th...

How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers

One of my pet hates of C-derived languages (as a mathematician) is that (-1) % 8 // comes out as -1, and not 7 fmodf(-1,8) // fails similarly What's the best solution? C++ allows the possibility of templates and operator overloading, but both of these are murky waters for me. examples gratefully received. ...

Operator-function + with two implicit casts doesn't work

hi, I'm trying to port some parts from ginac (www.ginac.de) to C#. But I encountered this: class Program { static void Main(string[] args) { symbol s = new symbol(); numeric n = new numeric(); ex e = s + n; // "Operator + doesn't work for symbol, numeric" } } class ex { //this should be alre...

how to overload == operator to allow it to be used in multiple comparisons ?

Hi, I am trying to overload == operator to compare objects like below. class A { int a; public: A(int x) { a = x; } bool operator==(const A& obRight) { if(a == obRight.a) { return true; } return false; } }; int main() { A ob(10), ob2(10), ob3(10); if(ob == ob2) // ...

overloading ostream for any function that returns a vector

Hi assume that I have class A : using namespace std; template <class T> class A{ private: vector<T> my_V; public: // assume initializations etc are done inline vector<T> get_v() { return my_v; } }; and some where else I have overloaded ostream of std::vector template <class T> ostream & operator<<(ostream& out, vector<T> &vec...

Overload comparison operators for a templated class

Hi experts, I'm having troubles in overloading comparison operators in order to compare two pair struct in such way: typedef pair<string, unsigned int> INDEX; bool operator>(INDEX &v1, INDEX &v2) { if(v1.second == v2.second) //if integer parts are equal { //string that comes earlier in the dictionary should be larger ...

Can't overload << for Enum in C++

I have created an enumerated data type to define possible flight lengths. I'd like to overload its << operator so the representation is nicer. When I compile this, I get the following error (posted for completeness' sake. Basically multiple definitions of operator <<(ostream&, Categoria&)): g++ -oProjectoAEDA.exe src\voo.o src\tui.o sr...

Operator overloading on class templates

I'm having some problems defining some operator overloads for template classes. Let's take this hypothetical class for example. template <class T> class MyClass { // ... }; operator+= // In MyClass.h MyClass<T>& operator+=(const MyClass<T>& classObj); // In MyClass.cpp template <class T> MyClass<T>& MyClass<T>::operator+=(const ...

operator<< for nested class

I'm trying to overload the << operator for the nested class ArticleIterator. // ... class ArticleContainer { public: class ArticleIterator { // ... friend ostream& operator<<(ostream& out, const ArticleIterator& artit); }; // ... }; If I define operator<< like I usual...

Parameter issue with operator overload for !=

I'm trying to define an overload for the != operator. My code is as follows. (Update: outdated code. If one of two article pointers points to NULL, this code will crash.) bool ArticleContainer::ArticleIterator::operator!=(const ArticleIterator& artit) { if (this->article == NULL && artit.article == NULL) return false; el...