I overloaded both subscript operator and assignment operator  and I am trying to get right value to assignment operator
example
 Array x;
x[0]=5;
by overloading subscript operator i can get value 0 but when i overload assignment operator it does the assignment but it doesn't use my overloaded function because vaiable 2 should have value 5.
class Array
{
public:
    int *ptr;
    int one,two;
    Array(int arr[])
    {
        ptr=arr;
    }
    int &operator[](int index)
    {
        one=index;
        return ptr[index];
    }
    int & operator=(int x){
        two=x;
        return x;
    }   
};
int main(void)
{
    int y[]={1,2,3,4};
    Array x(y);
    x[1]=5;
    cout<<x[0]<<endl;
}
                The subscript operator [] is normally used to access array elements. This operator can be overloaded to enhance the existing functionality of C++ arrays.
Use a subscript operator to access one or more elements in an array. You can access a specific element or a range of elements in an array.
There isn't a double-subscript operator in C++. What you can do is overload operator[] to return an object that also overloads operator[] . This will enable you to write m[i][j] .
Overloading Subscript or array index operator [] in C++The Subscript or Array Index Operator is denoted by '[]'. This operator is generally used with arrays to retrieve and manipulate the array elements. This is a binary or n-ary operator and is represented in two parts: postfix/primary expression.
How many arguments will the subscript operator will take for overloading? Explanation: The subscript operator overload takes only one argument, but it can be of any type.
Overloading the assignment operator (operator=) is fairly straightforward, with one specific caveat that we’ll get to. The assignment operator must be overloaded as a member function. This should all be pretty straightforward by now. Our overloaded operator= returns *this, so that we can chain multiple assignments together:
Subscripting [] Operator Overloading in C++. The subscript operator [] is normally used to access array elements. This operator can be overloaded to enhance the existing functionality of C++ arrays. Following example explains how a subscript operator [] can be overloaded. When the above code is compiled and executed, ...
1 For operator overloading to work, at least one of the operands must be a user defined class object. 2 Assignment Operator: Compiler automatically creates a default assignment operator with every class. ... 3 Conversion Operator: We can also write conversion operators that can be used to convert one type to another type. ... More items...
However, when you overload a binary operator, the corresponding compound assignment operator, if any, is also implicitly overloaded. For example, += is evaluated using +, which can be overloaded. These operators cannot be overloaded.
It does not use your operator= because you are not assigning to an instance of Array, you're assigning to an int. This would invoke your operator:
Array x;
x = 7;
If you want to intercept assignments to what operator[] returns, you must have it return a proxy object and define the assignment operator for that proxy. Example:
class Array
{
  class Proxy
  {
    Array &a;
    int idx;
  public:
     Proxy(Array &a, int idx) : a(a), idx(idx) {}
     int& operator= (int x) { a.two = x; a.ptr[idx] = x; return a.ptr[idx]; }
  };
  Proxy operator[] (int index) { return Proxy(*this, index); }
};
                        If you remove the overloading of the = operator in your code, then you'll already have the behaviour you're desiring, since your overloaded [] operator returns a reference to the list item.  For example:
#include <iostream>
using namespace std;
template<typename T>
class Array {
    private:
        // This method is used to reallocate our array when the number of elements becomes equal to the length of the array.
        void _grow() {
            length *= 2;
            T* temp = new T[length];
            // Copy over the array elements
            for (int i = 0; i <= current; i++) {
                temp[i] = items[i];
            }
            // Delete the old array
            delete [] items;
            // Set the array pointer equal to the pointer to our new memory, then annihilate the temp pointer
            items = temp;
            temp = NULL;
        }
    public:
        unsigned int length, current;
        T* items;
        // Constructor and destructor
        Array() {
            current = 0;
            length = 128;
            items = new T[length];
        }
        ~Array() {
            delete [] items;
            items = NULL;
        }
        // Overload the [] operator so we can access array elements using the syntax L[i], as in Python
        T& operator[] (unsigned int i) {
            return items[i];
        }
        // Add items to the Array if there is room.  If there is no room, grow the array and then add it. 
        void push(T b) {
            if (current + 1 < length) {
                items[current] = b;
                current += 1;
            } else {
                _grow();
                items[current] = b;
                current += 1;
            }
        }
};
int main() {
    Array<int> L;
    L[0] = 10;
    L[1] = 15;
    std::cout << L[0] << endl;
    std::cout << L[1] << endl;
    return 0;
}
Output:
jmracek:include jmracek$ g++ Array.cpp -o a
jmracek:include jmracek$ ./a
10
15
If I were being careful, I would also include error handling for the case where I try and $L[i]$ and element outside of the array length.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With