Questions tagged [operator-overloading]

Operator overloading is a feature of a programming language that allows custom implementations for operators depending on the types of the operands involved. Some languages allow new operators to be defined while others only allow redefinition of existing ones.

Operator overloading is a feature of a programming language that allows custom implementations for operators depending on the types of the operands involved. Some languages allow new operators to be defined while others only allow redefinition of existing ones.

Popular questions

See also

7743 questions
3
votes
1 answer

Can't find error in template class

template< typename T > double GetAverage(T tArray[], int nElements) { T tSum = T(); // tSum = 0 for (int nIndex = 0; nIndex < nElements; ++nIndex) { tSum += tArray[nIndex]; } // convert T to double return double(tSum) /…
3
votes
1 answer

How do name-lookup and operator-overload work?

I want to output some private library class ns::A to the plog, so I add the operator << overload to ns::A. The following code cannot be compiled. error: no match for ‘operator<<’ (operand types are ‘std::ostringstream’ {aka…
user2709407
  • 320
  • 3
  • 10
3
votes
1 answer

Subscript (" [ ] ")operator gives strange errors

I am getting some strange behaviour from visual studio, regarding the following code snippet, The error list shows several instances of E0349: no operator"[]" matches these operands. Intellisense seems to be implying a type mismatch, but as you…
Ian Young
  • 1,464
  • 1
  • 14
  • 26
3
votes
1 answer

Copy constructor and overloaded addition operator

I am reviewing operator overloading in C++. Just for fun I am implementing a BigInt class. The first operator I want to overload for it is the addition operator. I have decided to overload this operator as a friend non-member function. Here's a MWE…
3
votes
3 answers

Is there a way to compound functions in C++?

General question : If there are two objects A and B with respective functions f_A(arg list) and f_B(arg list). What's the best way to create an object C with a function compounded of f_A(...) and f_B(...) ? for example : f_C() = f_A() + f_B() or…
Cryckx
  • 465
  • 4
  • 12
3
votes
1 answer

C# - Can't override ==

I have the following class (built for Unity Game Engine) using System; using System.Collections.Generic; using UnityEngine; public class Biome : ScriptableObject, IEquatable { // ... // // IEquatable // public bool…
Enrique Moreno Tent
  • 21,095
  • 29
  • 89
  • 173
3
votes
2 answers

operator overloading multiple operands C++

I have coded NumberS class, it is just for demonstration. I overloaded the operator+. It is OK when applying the operator with 2 operands, but applying with more operands producted undefined result. The code is below NumberS.h #include…
3
votes
2 answers

Operator Overloading with Tuples in Swift

Operator Overloading Swift 4.1, Xcode 9.3 I am trying to make a quadratic equation function in Swift. While working on this, I found that I needed to overload some operators so that I could work with tuples alongside other numbers (Double in this…
Noah Wilder
  • 1,369
  • 14
  • 31
3
votes
1 answer

<< operator override compiles with g++ not windows

I am trying to port an application to win-dows (ironic, I know). The following bare-bone example illustrates the problem. I get the following error when compiling with VS12 and VS14: C2679 binary '<<': no operator found which takes a right-hand…
atomSmasher
  • 1,425
  • 2
  • 15
  • 34
3
votes
2 answers

Adding overload operator == with enable_if

I have a working template class point with an overload for operator==. Because of floating point comparison, I was trying to add a second overload with enable_if for floating point to use an almost equal function. This is my…
Moia
  • 1,773
  • 7
  • 24
3
votes
1 answer

C++ overloading: switching from friend to member function

I have this code which I wish to switch from friend functions to member functions: inline bool operator< (const MyClass& left, const MyClass& right) { return (((left.value == 1) ? 14 : left.value) < ((right.value == 1) ? 14 :…
3
votes
1 answer

C++ << operator overloading

As per Deitel's 9ed C++ How to program, p. 444: Why Overloaded Stream Insertion and Stream Extraction Operators Are Overloaded as Non-Member Functions The overloaded stream insertion operator (<<) is used in an expression in which the left…
J. Doe
  • 421
  • 3
  • 11
3
votes
0 answers

C# operator overloading not working when passed as generic parameter

I added operators for == and != to a class but it seems that they are not called when a object of that type is passed as a generic parameter. When I look at the decompiled code it looks like (object)a == (object)b. Is it possible to use the operator…
3
votes
1 answer

Is it okay to use an overloaded operator to implement another operator overload?

For instance if I have overloaded a + operator myClass & operator + (const myClass & rhs) and also overloaded = operator myClass & operator = (const myClass & rhs) both operators are working fine. Can I use this overloaded operator in my +=…
Mykel
  • 121
  • 7
3
votes
1 answer

Can I overload an operator taking a pointer and a non-constructed template object?

I would like to write some code like this: some_container | foo; For concreteness, let's say the RHS is foo<2>. The tricky part is, foo is something that should be automatically instantiated. IOW, I "fail" if foo<2> has to be explicitly…
Matthew
  • 2,121
  • 18
  • 20
1 2 3
99
100