operator-overloading

subscript operator on pointers

If i have a pointer to an object that has an overloaded subscript operator ( [] ) why cant i do this: MyClass *a = new MyClass(); a[1]; but have to do this instead: MyClass *a = new MyClass(); (*a)[1]; ...

Assign method in Scala.

When this code is executed: var a = 24 var b = Array (1, 2, 3) a = 42 b = Array (3, 4, 5) b (1) = 42 I see three (five?) assignments here. What is the name of the method call that is called in such circumstances? Is it operator overloading? Update: Can I create a class and overload assignment? ( x = y not x(1) = y ) ...

Lua Operator Overloading

I've found some places on the web saying that operators in Lua are overloadable but I can't seem to find any example. Can someone provide an example of, say, overloading the + operator to work like the .. operator works for string concatenation? EDIT 1: to Alexander Gladysh and RBerteig: If operator overloading only works when both ope...

Overload operator in F#

Hi, I would like to overload the (/) operator in F# for strings and preserve the meaning for numbers. /// Combines to path strings let (/) path1 path2 = Path.Combine(path1,path2) let x = 3 / 4 // doesn't compile If I try the following I get "Warning 29 Extension members cannot provide operator overloads. Consider defining the operat...

Explain this C++ operator definition

I have the following operator defined in a C++ class called StringProxy: operator std::string&() { return m_string; } a) What is this and how does this work? I understand the idea of operator overloading, but they normally look like X operator+(double i). b) Given an instance of StringProxy, how can I use this operator to get the...

Comparing objects and inheritance

Hi, In my program I have the following class hierarchy: class Base // Base is an abstract class { }; class A : public Base { }; class B : public Base { }; I would like to do the following: foo(const Base& one, const Base& two) { if (one == two) { // Do something } else { // Do something else } } I have issues ...

C++ Beginner - 'friend' functions and << operator overloading: What is the proper way to overload an operator for a class?

Hello, everyone! In a project I'm working on, I have a Score class, defined below in score.h. I am trying to overload it so, when a << operation is performed on it, _points + " " + _name is returned. Here's what I tried to do: ostream & Score::operator<< (ostream & os, Score right) { os << right.getPoints() << " " << right.scoreGetN...

Getting the compiler to perceive << as defined for a specific class

Hello everyone. I edited a post of mine with this question, yet got no answers. I overloaded << for a class, Score (defined in score.h), in score.cpp. ostream& operator<< (ostream & os, const Score & right) { os << right.getPoints() << " " << right.scoreGetName(); return os; } (getPoints fetches an int attribute, getName a string ...

how to call operator () in c++

in c++ i have following code class Foobar{ public: Foobar * operator()(){ return new Foobar; }; My quesion is how to call the (); if i do Foobar foo() the constructor gets called i am confused about behaviour of () can some explain me ...

How to call a templated operator overload without using the word 'operator'?

class RunAround; class HopUpAndDown; class Sleep; template<typename Acts> int doThis(); template<> int doThis<RunAround>() { /* run run run.. */ return 3; } template<> int doThis<HopUpAndDown>() { /* hop hop hop.. */ return 2; } template<> int doThis<Sleep>() { /* zzz.. */ return -2; } struct Results { template<typenam...

typedef and operator overloading in C++

Suppose I typedef an integer or integer array or any known type: typedef int int2 Then I overload operator * for int2 pairs, now if I initialize variables a and b as int. Then will my * between a and b be the overloaded * ? How do I achieve overloading an int and yet also use * for int the way they are. Should I create a new type? ...

CString a = "Hello " + "World!"; Is it possible?

I'm making my own string class and I was wondering if there's any way to replace the compiler behaviour around " characters. As I said in the title I'd like to make it so that CString a = "Hello " + "World!"; would actually work and not give a compiler error telling that it can't add 2 pointers. My string class automatically converts to ...

How can I overload the [] operators?

I'm creating a class which populates a dictionary as a private member. I want to expose the values of the dictionary without exposing the dictionary itself (in a read-only fashion.) I can easily make a property to expose _dictionary.Keys, but how can I overload [] so that MyClass[key] returns _dictionary[key]? I think I may need to impl...

override the operator overloading in C++ ?

helo guys i have class call Complex I did operator overloading like such Complex c = a + b; // where a and b are object of Complex class which basically is operator+(Complex& that); but I dont know how to say for example double c = a + 10; //where a is object of Complex class but 10 is integer / double I did define type...

C++ overloading virtual = operator

Hello, here is the code for my question: class ICommon { public: virtual ICommon }; class CSpecial : public ICommon { public: CSpecial } }; CSpecial obj; Basically: I want the interface ICommon to force it's descendants to implement = operator but don't want to have any typecasts in the implementation. The compiler says "can't ...

Which programming languages, other than C++, support operator overloading?

I know C++. Anything other? ...

Visual C++ doesn't see operator<< overload

I have a vector class that I want to be able to input/output from a QTextStream object. The forward declaration of my vector class is: namespace util { template <size_t dim, typename T> class Vector; } I define the operator<< as: namespace util { template <size_t dim, typename T> QTextStream& operator<<(QTextStream& out, const ut...

Simple way to overload compound assignment operator in C#?

Does anyone have a very simple example of how to overload the compound assignment operator in C#? ...

Unoverloadable C++ operators

What operators can not be overloaded in C++? ...

Visitor and templated virtual methods

In a typical implementation of the Visitor pattern, the class must account for all variations (descendants) of the base class. There are many instances where the same method content in the visitor is applied to the different methods. A templated virtual method would be ideal in this case, but for now, this is not allowed. So, can te...