Discussion Locked

C++ [] operator overload

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I want to do something very simple. I want an object where I define two subscript operator (i.e. [] ) functions. I want one that will return to the user a value that is modifiable and one that will return to the user a value that can not be modified so that if I do this:

Object a = b[2];

The "read only" function will get called.
And if I do this...

b[2] = new Object.

The "write" function will get called.

7 replies

by jaqui

set up your two functions first off, save as a library file to be included in the app.

use an if then statement to test against what you want.

use a variable that gets set when you activate the " b[2] " if read only,
test that variable:

if$s (isset)
(read only funtion)
else
(write funtion)

that calls the function you want.

by solrak29

So your saying that in my overloaded operator function, I check to see if there is a value at my location b[2]....
if there is a value there then call the read only function
else
call the write function?

I cant use that as an indicator if the user's intention is to set or get data from the array.... the only indication is that the user is assigning....b[2]= aObject. Or the user is retrieving data aObject = b[2]. Now in retireving I want to return a const reference and in assigning I already return the refernce so it gets assigned. The return of the const reference is the trick.....

by alnikolov

The current version of the C++ standard will not allow you to do such thing. In order to overload a function (and overloading operator[] is the same) the overloaded functions must differ by the number or/and the type of the parameters.

for example:
int foo(int a);
bool foo(int a); is not a valid overload because the functions only differ by the type of the return value.

But if you are using VC++ (version 6 and later) you can use specific Microsoft extension and you can do this:

class B
{
public:
operator=(const B&)
{
}
};

class A
{
B m_b;
public:
__declspec( property( get=getB, put=putB) ) B b[]; // Microsoft specific extension. Not standard C++.

const B& getB(int i)
{
return m_b;
}

void putB(int i, B& aVal)
{
}
};

int main(int argc, char* argv[])
{
A a;
B b;
a.b[4] = b; // will call the set method for b
B const & c = a.b[4]; // will call the get method for b
return 0;
}

by solrak29

Poster rated this answer.

Ahhh, thank you, I was under the impression by using const that you can make such a thing happen. But the using of the MS extension is good to know...you wouldn't know how to implement this would you on another platform?

What about:?

const foo operator[]( const foo ) const;
foot operator[](const foo);

Answer this and I will close this question to you.

by alnikolov

For some reason I cannot add comment to my prewious answer so I am submitting a new answe:

the "property" extension is specific to Visual C++ from version 6. there is simmilar extension in Borland C++ Builder and Borland ObjectPascal - the sintax is different but the behavior is the same. All this extensiona are compiler dependant and they translate to substituting the "." operator with a call to the get or set function. In C++ you cannot overload ".". That is why I do not think it is possible to implement this in standard C++.

As for the second part of your comment:
"const" function is a function that does not change any of the class members and can call only "const" function. You can operload operator[] like that, but it will not ser your goals, or at least not directly. Look at the following code example and comments:
class B
{
public:
};

class A
{
B m_b; // using this just to have some return value available
public:
const B& operator[](int i) const { return m_b; }
B& operator[](int i){ return m_b; }
};

int main(int argc, char* argv[])
{
A a;
A const & a1 = a;
B b;
// the non const version of "operator[]" will be used as "a" is not constant.
a[4] = b;
B const & d = a[4];

// the const version of "operator[]" will be used as a1 is a const&
B const & c = a1[4]; // as a1 is "const&" the const version of operator[] will be used

// but it is possible to do this
// we convert "a" to a const& so we force the "operator[]() const" to be used.
// but i am not sure that this is what you want to do
B const & e = ((A const&)a)[4];

return 0;
}

In any case I will recommend to abenden the idea of overloading operator []. Instead implement "get" and "set" methods that deliver the reqiored functionality. This way you will avoid the confusion the overloaded [] will bring to the users of your classes.

by solrak29

Poster rated this answer.
Thanks. I have abandon the idea and moved on, but it very interesting what you have written.

This discussion is closed to new replies.