Archived

This topic is now archived and is closed to further replies.

operator[]

This topic is 5703 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


          
class MyClass
{
public:
// constructors/destructors etc

int& operator[]( int index )
{
return v_[index];
}

private:
std::vector<int> v_;
};


operator [] doesn't have to take an integer as a parameter either. You can do this as well:


                    
class MyClass
{
public:
// constructors/destructors etc

int& operator[]( string name )
{
return m_[name];
}

private:
std::map<string, int> m_;
};


This allows you pass a string to the subscript operator. Heck, you can even pass a class as a parameter if you want. Obviously, these two examples are very trivial, and do not perform any error checking, but hopefully it gives you the right idea.

Dire Wolf
www.digitalfiends.com

[edited by - Dire.Wolf on May 2, 2002 7:04:58 PM]

Share this post


Link to post
Share on other sites
Well, there''s good news and bad news:

Bad news:
You cannot overload the [] operator for use in assignment, as you have shown here:
myclass[10] = 6;

The good news is that you can overload it as an accessor, as Dire.Wolf explained. That would work for your second example:
x = myclass[5];

Share this post


Link to post
Share on other sites
yea, there should be a []= operator.

you could though, create a subclass, with an overloaded operator=, and have an array of those accessed through the ur brackets. that way, it sets itself.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you overload the operator[] the way that Dire.Wolf did, you did overload its use in assignment. Since his operator[] overload returns a reference, that return value can be on the left hand side of an assignment operator.

Share this post


Link to post
Share on other sites
yea, there should be a []= operator.

There should be, but there isn't.

you could though, create a subclass, with an overloaded operator=, and have an array of those accessed through the ur brackets. that way, it sets itself.

I am interested in this idea, since I have never heard it before. Please explain, but use English this time. I have no idea what the 'ur brackets' are. Have you considered that fact that no matter what happens with your expression, you still have to deal with the [] by themselves (independent of the assignment operator)?

i.e.:
int bar, doc;
class foo[bar]= (class)fern[doc];

will call the overloaded =, then the overloaded [] so you still have to deal with the []. Notice that both [bar] and [doc] are going to be treated the same in either case, even though you want a set and a get.

Anyways, I do not think you can turn [] into a setter. Good luck, though.

[edited by - jeeky on May 2, 2002 7:23:58 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by jeeky
Bad news:
You cannot overload the [] operator for use in assignment, as you have shown here:
myclass[10] = 6;


Look closely at Dire.Wolf's code. operator[] returns a *reference* to an int. Hence you *can* use it like this:

myclass[10] = 6;

[EDIT: Didn't see your reply there Anon]

[edited by - Dactylos on May 2, 2002 7:25:56 PM]

Share this post


Link to post
Share on other sites
Can you do this?

myclass[10] = 6;

or do you have to do this?

myclass.[10] = 6;

If you are correct, I am happy to be wrong about it.

Share this post


Link to post
Share on other sites
quote:
Original post by jeeky
Can you do this?

myclass[10] = 6;


Yes, you can, as long as operator[] returns a reference and not simply a value.
quote:

or do you have to do this?

myclass.[10] = 6;


No, you can''t do that. But if you want to you can do this:

myclass.operator[](10) = 6;

Share this post


Link to post
Share on other sites
Heh, thanks Dactylos & AP

Heck if you wanted to you could even create a class to hold the return value of operator [] to do all sorts of fancy stuff, like synchronized read/write via temporary variables (a wrapper object returned by value.) You see these classes used frequently when overloading operator -> and operator * for use in smart pointers with synchronized access.

There is a great example of using a helper/wrapper class in Bjarne Stroustrups, "The C++ Programming Language 3rd Edition", page 295:


class String {
// ...

Cref operator[](int i)
{
check(i);
return Cref(*this, i);
}

char operator[](int i) const
{
check(i);
return rep->s[ i ];
}

class Srep;
Srep* rep; // shared string representation
};


By returning a Cref object, as soon as a character is assigned to the string, a copy is made (via Cref's overloaded operator = and an internal reference to its parent string.) This is known as copy-on-write access and is implemented in C++ by returning a class object this way.

Regards,

Dire Wolf
www.digitalfiends.com


[edited by - Dire.Wolf on May 2, 2002 11:18:36 PM]

[edited by - Dire.Wolf on May 3, 2002 10:41:32 AM]

Share this post


Link to post
Share on other sites