operator-overloading

How would you overload the [] operator in javascript

I can't seem to find the way to overload the [] operator in javascript. Anyone out there know? I was thinking on the lines of ... MyClass.operator.lookup(index) { return myArray[index]; } or am I not looking at the right things. Thanks in advanced ...

Overloading the ostream << operator for a static class?

I have a (simplified) static global class and << operator overload as follows: class Global { private: static int counter; Global(){}; public: friend ostream& operator<<(ostream &out, Global &global); } ostream& operator<< (ostream &out, Global &global) { //... do output return out; } I want to be able to...

Overloading Insertion Operator in C++

I have a class in which I'm trying to overload the << operator. For some reason, it is not being overloaded. Here is my .h file: friend std::ostream& operator<<(std::ostream&, const course &); //course is my class object name in my .cpp, I have this as my implementation: std::ostream& operator<<(std::ostream &out, const course & rhs...

Why is this syntax invalid? vectorPointer->[0]

In C++, why is the following element access in a vector invalid? void foo(std::vector<int>* vecPtr) { int n = vecPtr->size(); // ok int a = vecPtr->[0]; // invalid } Instead, we have to write the more cumbersome (*vecPtr)[0] = 1; I think, the operator[] call should just have the same syntax like a method call, and I hate t...

c++ error: operator []: 2 overloads have similar conversions

template <typename T> class v3 { private: T _a[3]; public: T & operator [] (unsigned int i) { return _a[i]; } const T & operator [] (unsigned int i) const { return _a[i]; } operator T * () { return _a; } operator const T * () const { return _a; } v3() { _a[0] = 0; // works _a[1] = 0; _a[2] = 0; ...

templated class can't redefine operator[]

I've this class namespace baseUtils { template<typename AT> class growVector { int size; AT **arr; AT* defaultVal; public: growVector(int size, AT* defaultVal ); //Expects number of elements (5) and default value (NULL) AT*& operator[](unsigned pos); int length(); void reset(int pos); //Reset...

Does a destructor always get called for a delete operator, even when it is overloaded?

I'm porting a bit of an old code from C to C++. The old code uses object-like semantics, and at one point separates object destruction from freeing the now-unused memory, with stuff happening in between: Object_Destructor(Object *me) { free(me->member1), free(me->member2) } ObjectManager_FreeObject(ObjectManager *me, Object *obj) { fre...

templated operator() overload C++

someone already asked this question, but the thread ended up with the original question not getting answered. suppose you have this: template<size_t i, class f_type> void call_with_i(f_type f); functor_type is either: a) a struct with a method that has the following signature: template<size_t i> operator()() const; or, b) a fun...

C# Operator overloading - towards practical

Most of the websites,articles i have gone through explains operator overloading by giving the following standard example. class Complex { int real; int imaginary; public Complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; } public...

C# overloading operator== versus Equals()

I'm working on a C# project for which, until now, I've used immutable objects and factories to ensure that objects of type Foo can always be compared for equality with ==. Foo objects can't be changed once created, and the factory always returns the same object for a given set of arguments. This works great, and throughout the code base ...

C++ overloading operator= in template

Hi all I'm having trouble with C++ template operator= What I'm trying to do: I'm working on a graph algorithm project using cuda and we have several different formats for benchmarking graphs. Also, I'm not entirely sure what type we'll end up using for the individual elements of a graph. My goal is to have a templated graph class and a...

What is ->* operator in C++?

C++ continues to surprise me. Today i found out about the ->* operator. It is overloadable but i have no idea how to invoke it. I manage to overload it in my class but i have no clue how to call it. struct B { int a; }; struct A { typedef int (A::*a_func)(void); B *p; int a,b,c; A() { a=0; } A(int bb) { b=b; c=b; } ...

C# Extend array type to overload operators

I'd like to create my own class extending array of ints. Is that possible? What I need is array of ints that can be added by "+" operator to another array (each element added to each), and compared by "==", so it could (hopefully) be used as a key in dictionary. The thing is I don't want to implement whole IList interface to my new clas...

Overload template relational operators

I'm having a problem with a template ARRAY class. I have another Rational class that i added to this ARRAY class. what i need it to do is take in rational numbers as fractions (exp 1/2) and sort them. i believe i need to overload the relational operators but thats where im stuck. do i over load them in the ARRAY class or the Rational Cla...

Overloading operator<< for a templated class

Hello! I'm trying to implement a method for a binary tree which returns a stream. I want to use the stream returned in a method to show the tree in the screen or to save the tree in a file: These two methods are in the class of the binary tree: Declarations: void streamIND(ostream&,const BinaryTree<T>*); friend ostream& operator<<(ost...

help overloading << and >> to display two values

This may be a novice question, but I can't figure it out by inspecting the book I have. The class's constructor initializes two doubles, and I want the following code to output those two doubles with <<. Complex x( 3.3, 1.1 ); cout << "x: " << x; After this I need to overload >> to accept two doubles into these. This is my first ques...

How do you overload the << operator in Ruby?

I'm not sure how to accomplish overloading the << operator for a method. This is how I assumed it would work: def roles<<(roles) ... end That however, throws errors. Any suggestions? ...

Const correctness in C++ operator overloading returns

Hi! I'm a little confused as to why I've been told to return const foo from a binary operator in c++ instead of just foo. I've been reading Bruce Eckel's "Thinking in C++", and in the chapter on operator overloading, he says that "by making the return value [of an over-loading binary operator] const, you state that only a const member f...

What on earth would compell C++ to call this function?

I'm working on a programming language that uses C++ as it's target language for now. I'm hitting an exceptionally strange backtrace. #1 0x08048d09 in factorial (n=0x8052160) at ir.cpp:35 35 shore::builtin__int * __return = NULL; (gdb) bt #0 shore::builtin__int::__mul__ (this=0x8052160, other=0x8052288) at /home/alex/projects/sho...

C# How to make a generic class?

How can I make this generic? class AtomicReference { private Object _value; public AtomicReference() { _value = new Object(); } public AtomicReference(Object value) { OptimisticSet(value); } public Object CompareAndSet(Object newValue) { return Interlocked.Exchange(ref _valu...