Asked  7 Months ago    Answers:  13   Viewed   31 times

Is it possible to overload [] operator twice? To allow, something like this: function[3][3](like in a two dimensional array).

If it is possible, I would like to see some example code.

 Answers

44

You can overload operator[] to return an object on which you can use operator[] again to get a result.

class ArrayOfArrays {
public:
    ArrayOfArrays() {
        _arrayofarrays = new int*[10];
        for(int i = 0; i < 10; ++i)
            _arrayofarrays[i] = new int[10];
    }

    class Proxy {
    public:
        Proxy(int* _array) : _array(_array) { }

        int operator[](int index) {
            return _array[index];
        }
    private:
        int* _array;
    };

    Proxy operator[](int index) {
        return Proxy(_arrayofarrays[index]);
    }

private:
    int** _arrayofarrays;
};

Then you can use it like:

ArrayOfArrays aoa;
aoa[3][5];

This is just a simple example, you'd want to add a bunch of bounds checking and stuff, but you get the idea.

Tuesday, June 1, 2021
 
Drazisil
answered 7 Months ago
34

No, Java doesn't support user-defined operator overloading. The only aspect of Java which comes close to "custom" operator overloading is the handling of + for strings, which either results in compile-time concatenation of constants or execution-time concatenation using StringBuilder/StringBuffer. You can't define your own operators which act in the same way though.

For a Java-like (and JVM-based) language which does support operator overloading, you could look at Kotlin or Groovy. Alternatively, you might find luck with a Java compiler plugin solution.

Tuesday, June 1, 2021
 
Marcelo
answered 7 Months ago
29

If you define your operator overloaded function as member function, then the compiler translates expressions like s1 + s2 into s1.operator+(s2). That means, the operator overloaded member function gets invoked on the first operand. That is how member functions work!

But what if the first operand is not a class? There's a major problem if we want to overload an operator where the first operand is not a class type, rather say double. So you cannot write like this 10.0 + s2. However, you can write operator overloaded member function for expressions like s1 + 10.0.

To solve this ordering problem, we define operator overloaded function as friend IF it needs to access private members. Make it friend ONLY when it needs to access private members. Otherwise simply make it non-friend non-member function to improve encapsulation!

class Sample
{
 public:
    Sample operator + (const Sample& op2); //works with s1 + s2
    Sample operator + (double op2); //works with s1 + 10.0

   //Make it `friend` only when it needs to access private members. 
   //Otherwise simply make it **non-friend non-member** function.
    friend Sample operator + (double op1, const Sample& op2); //works with 10.0 + s2
}

Read these :
A slight problem of ordering in operands
How Non-Member Functions Improve Encapsulation

Tuesday, June 1, 2021
 
Litty
answered 7 Months ago
46

As other answers have mentioned, you can indeed overload operators (by definining special methods in the class you're writing, i.e., methods whose names start and end with two underscores). All the details are here.

To complete the answers to you questions: you cannot define new operators; but << is not a new operator, it's an existing one, and it's overloaded by defining in the class the method __lshift__.

As a historical note, this is also pretty much the situation in C++ -- but the exact set of operators you can overload differs between the two languages. For example, in C++, you cannot overload attribute access, .; in Python, you can, with __getattr__ (or __getattribute__, with different semantics) and __setattr__. Vice versa, in Python = (plain assignment) is not an operator, so you cannot overload that, while in C++ it is an operator and you can overload it.

<< is an operator, and can be overloaded, in both languages -- that's how << and >>, while not losing their initial connotation of left and right shifts, also became I/O formatting operators in C++ (not in Python!-).

Thursday, June 3, 2021
 
jedwards
answered 6 Months ago
12

This is not currently possible, because extension methods must be in static classes, and static classes can't have operator overloads. But the feature is being discussed for some future release of C#. Mads talked a bit more about implementing it in this video from 2017.

On why it isn't currently implemented, Mads Torgersen, C# Language PM says:

...for the Orcas release we decided to take the cautious approach and add only regular extension methods, as opposed to extention properties, events, operators, static methods, etc etc. Regular extension methods were what we needed for LINQ, and they had a syntactically minimal design that could not be easily mimicked for some of the other member kinds.

We are becoming increasingly aware that other kinds of extension members could be useful, and so we will return to this issue after Orcas. No guarantees, though!

Further below in the same article:

I am sorry to report that we will not be doing this in the next release. We did take extension members very seriously in our plans, and spent a lot of effort trying to get them right, but in the end we couldn't get it smooth enough, and decided to give way to other interesting features.

This is still on our radar for future releases. What will help is if we get a good amount of compelling scenarios that can help drive the right design.

Tuesday, June 8, 2021
 
tika
answered 6 Months ago
53
// In MyClass.h
MyClass<T>& operator+=(const MyClass<T>& classObj);


// In MyClass.cpp
template <class T>
MyClass<T>& MyClass<T>::operator+=(const MyClass<T>& classObj) {
  // ...
  return *this;
}

This is invalid for templates. The full source code of the operator must be in all translation units that it is used in. This typically means that the code is inline in the header.

Edit: Technically, according to the Standard, it is possible to export templates, however very few compilers support it. In addition, you CAN also do the above if the template is explicitly instantiated in MyClass.cpp for all types that are T- but in reality, that normally defies the point of a template.

More edit: I read through your code, and it needs some work, for example overloading operator[]. In addition, typically, I would make the dimensions part of the template parameters, allowing for the failure of + or += to be caught at compile-time, and allowing the type to be meaningfully stack allocated. Your exception class also needs to derive from std::exception. However, none of those involve compile-time errors, they're just not great code.

Thursday, June 17, 2021
 
aaronhuisinga
answered 6 Months ago
99

Yes. For example, there is __radd__. Also, there are none for __le__(), __ge__(), etc., but as Joel Cornett rightly observes, if you define only __lt__, a > b calls the __lt__ function of b, which provides a workaround.

>>> class My_Num(object):
...     def __init__(self, val):
...         self.val = val
...     def __radd__(self, other_num):
...         if isinstance(other_num, My_Num):
...             return self.val + other_num.val
...         else:
...             return self.val + other_num
... 
>>> n1 = My_Num(1)
>>> n2 = 3
>>> 
>>> print n2 + n1
4
>>> print n1 + n2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'My_Num' and 'int'

Note that in at least some cases it's reasonable to do something like this:

>>> class My_Num(object):
...     def __init__(self, val):
...         self.val = val
...     def __add__(self, other_num):
...         if isinstance(other_num, My_Num):
...             return self.val + other_num.val
...         else:
...             return self.val + other_num
...     __radd__ = __add__
Saturday, June 26, 2021
 
dzm
answered 6 Months ago
dzm
45

I think you declared the Equals method like this:

public override bool Equals(BOX obj)

Since the object.Equals method takes an object, there is no method to override with this signature. You have to override it like this:

public override bool Equals(object obj)

If you want type-safe Equals, you can implement IEquatable<BOX>.

Tuesday, June 29, 2021
 
altexpape
answered 6 Months ago
53

The problem is the compiler cannot know if + operator can be applied to T. Unfortunately, there is no way to constraint T to be a numeric type in C#.

However, you might be able to workaround this using the dynamic runtime:

public class Complex<T> where T : struct
{
    public T _a, _b;
    public Complex(T i, T j)
    {
        _a = i;
        _b = j;
    }
    public static Complex<T> operator +(Complex<T> i, Complex<T> j)
    {
        return new Complex<T>(Sum(i._a, j._a), Sum(i._b, j._b));
    }

    private static T Sum(T a, T b)
    {
        return (dynamic)a + (dynamic)b;
    }
}
Tuesday, August 3, 2021
 
radmen
answered 4 Months ago
93

It should be in the bar namespace. You must consider what makes up the interface for the class, and group those together.

"A class describes a set of data along with the functions that operate on that data." Your free function operates on a Foo, therefore it is part of Foo. It should be grouped with Foo in the namespace bar.

Argument-dependent lookup, or ADL, will find the function.

We also know that we should prefer non-friend non-member functions. What this means is that, in general, your classes will have their definition and member functions, followed immediately by free functions which operate on the class.

Thursday, August 5, 2021
 
Kevin
answered 4 Months ago
57

Based on this question i wrote the fallowing code.
I did not completely understood function_traits ! but i was able to overload the >> operator for lambdas with different number of arguments . I know it's not the best possible solution , but i wrote it so someone can take it as a starting point ( a variadic template implementation will be awesome !) .

#include<iostream>
#include<string>
#include<tuple>
using namespace std;

template <typename T>
struct function_traits
: public function_traits<decltype(&T::operator())>
{};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const>
// we specialize for pointers to member function
{
    enum { arity = sizeof...(Args) };
    // arity is the number of arguments.

    typedef ReturnType result_type;

    template <size_t i>
    struct arg
    {
        typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
        // the i-th argument is equivalent to the i-th tuple element of a tuple
        // composed of those arguments.
    };
};

// a place holder class
class database_bind {};

template<int N>
class A {
    template<typename F>
    static void run(F l);
};

template<>
struct A<1> {
    template<typename F>
    static void run(F l) {
        typedef function_traits<decltype(l)> traits;
        typedef typename traits::arg<0>::type type_1;

        type_1 col_1;
        get_from_db(0, col_1);

    l(col_1);
    }
};
template<>
struct A<2> {
    template<typename F>
    static void run(F l) {
        typedef function_traits<decltype(l)> traits;
        typedef typename traits::arg<0>::type type_1;
        typedef typename traits::arg<1>::type type_2;

        type_1 col_1;
        type_2 col_2;
        get_from_db(0, col_1);
        get_from_db(1, col_2);

        l(col_1, col_2);
    }
};


void get_from_db(int col_inx, string& str) {  str = "string"; }
void get_from_db(int col_inx, int& i) {i = 123;}
void get_from_db(int col_inx, double& d) { d = 123.456; }


template<typename F>
void operator>>(database_bind dbb, F l)
{
    typedef function_traits<decltype(l)> traits;
    A<traits::arity>::run(l);
}

And finally :

int main() {
    database_bind dbb;

    dbb >> [](int i, string s) { cout << i << ' ' << s << endl; };
    dbb >> [](int i) { cout << i << endl; };
    dbb >> [](string s,double d) { cout << s << ' ' << d << endl; };
   }
Thursday, August 5, 2021
 
Jared_C
answered 4 Months ago
19

This is not operator overloading, this is marking a property as the default property of an object.

This is a COM feature, and it is merely supported by VB.

When an object that has a default property is used in a non-reference context (without Set), the value of the default property will be automatically used instead of the object pointer.

See here for more information (also follow the other links from there).

Saturday, August 7, 2021
 
phuongzzz
answered 4 Months ago
29

You have using std::ofstream instead of using std::ostream, so it doesn't know what ostream is.

You also need to include <ostream>.

Really, though, there's no reason to use using anything; you should just qualify the names with the namespace (especially if this is a header file, to avoid polluting the global namespace of other files):

friend std::ostream& operator<< (std::ostream&, const Dog&);
Sunday, November 28, 2021
 
inetbug
answered 4 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share