I'll get straight to the point as it's very difficult to do since I haven't found the answer on google or anywhere else.

Class ArrayOfTypes
{
	vector<MyType> P;

	&MyType operator [](int I) = (MyType& PT)
	{
	   if (P[I] != PT)
	   {
		  P[I] = PT;
	   }
	   return P[I];
	}
}

Anyone understand what I'm trying to do? I'm trying to overload both [] and = at the same time so that I can assign to a specific location in my vector P.

Recommended Answers

All 4 Replies

That just ain't how you do it, you need to implement (overload) ArrayOfTypes::operator[] and MyType::operator= separately to achieve what you want not try and do it together which is presumably this

ArrayOfTypes array;
MyType value;

array[10] = value;

ArrayOfTypes::operator[] returns a reference to a location in the array which can then be used to invoke MyType::operator= to assign to that location

class ArrayOfTypes
{
public: // ?
  vector<MyType> P;
     
  MyType& operator [](int I)
  {
    // Validate I in range ?
    return P[I];
  }

  // Implement a const version too for calling in const contexts to read the array
  const MyType& operator [](int I) const
  {
    // Validate I in range ?
    return P[I];
  }
};
class MyType
{
public:
  const MyType& operator=(const MyType& cpy)
  {
    // Don't copy self
    if (this != &cpy)
    {
      // Code to copy a MyType
    }

    return *this;
  }
};
commented: THANLS!! I solved it! +6

What would be the point of overloading two operators at the same time? Suppose you did what you were suggesting in your post - you would get functionality similar to data[x] = y . What happens if you want to read that variable now? There is no support for just data[x] and you cant assign to a variable either MyType m; m = x; The proper way to get the behavior you desire is to implement both functions. They are invoked separately in execution after all.

commented: Thanks for the input =] I solved it +6

But I already have what the Banfa posted.. it just keeps giving me some compiler error (OUT OF RANGE).. so that's why I assumed I was doing it wrong..

MyType& operator [](int I)
        {
			assert(I >= 0 && !MyType.empty());
            return P[I];
        }

		const MyType& operator [](int I) const
		{
			assert(I >= 0 && !MyType.empty());
            return P[I];
		}

and the Actual program that gives me the error:

#include <windows.h>
#include <iostream>
#include "Types.h"

using namespace std;

int main()
{
	ArrayOfTypes P;
        P[0] = MyType();       //Assertion error..
        cout<<P;

	cin.get();
	return 0;
}

So with that error, I removed the !MyType.isEmpty.. and I get subscript out of range.

You get an assertion error either because

  • Your asserts are poorly written and aren't actually performing a valid test assert(I >= 0 && !MyType.empty()); why test MyType when you are actually accessing P a vector<MyType>? surely assert(I < 0 P.size()); would be better or even better let the vector handle it instead of calling P[I] call P.at(I) . at is a member of vector that does the same as operator[] but range checks the input value first.
  • At line 10 you have access the array with first setting any size to it so 0 is out of bounds
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.