Expert Novice
Expert Novice

Reputation: 1963

Overloading of operator[]

What are the scenarios which may need to overload the subscript operator?

And what has the assert function got to do with this? I see in most cases of subscript overloading use of assert, would need an explanation on that.

Upvotes: 3

Views: 571

Answers (6)

JohnMcG
JohnMcG

Reputation: 8805

You might overload operator[] if you are implementing a class that is a collection or a wrapper for a collection. For me, I would only overload operator[] if the operation could be executed in constant or near-constant time, since that is how array indexing performs. It may make sense to use it to look up values in a hash table, but not to index into a linked list.

If you are finding asserts used in implementations of operator[], it is likely to enforce that the index argument is within the range of the collections. This, IMO, is an incorrect use of assert, since assert should be used to catch internal programming errors, rather than you program being called the wrong way. An end user or client application should never see an assertion error. The other possibility is that this class is only used internally, in which case an assertion error would reveal an internal programming error, not a wrong argument passed in by a client.

Upvotes: 1

user229044
user229044

Reputation: 239301

You might overload the [] operator on a custom container, to provide a syntactically/semantically clearer way of accessing elements.

For example my_container[3] = 9; is somewhat clearer than my_container.set(3, 9);

Of course, you could overload the [] to do essentially anything, but you probably shouldn't. You could, for example, cause my_object[3] to increment my_object by 3, but semantically the [] operator conveys lookup-by-index, and it's always better to have your interfaces conform to expectations.

You could use assert for quick-and-dirty bounds checking; it will cause your program to die messily, which is always preferable to introducing subtle memory corruption. The benefit is that assert is a macro which can be compiled out of production code, meaning you may pay the overhead of bounds-checking your container in development and not in production with no modification to your code.

Upvotes: 7

sehe
sehe

Reputation: 393084

Assert has absolutely nothing to do with the indexing operator operator[] per se.

Assert is used to check preconditions/invariants in debug builds, so violations of these are trapped before going into production.

Indexing operators are prone to index out of range type of situation, which might explain why you seem to be noticing them there more often than elsewhere. I use assert basically at least once in every parameterized function, and several times per class (checking invariants).

HTH

Upvotes: 1

You would consider overloading operator[] for classes where the operation (index( by a single field makes sense, as it is the case of vectors (index by position) set or maps (index by key). Note that if there is more than one dimension by which to index, it might make sense to use a different operator (operator()), as operator[] takes a single argument. You should probably read the C++FAQ lite entry on Operator Overloading.

In general assert is unrelated to operator[] (or any other operator). assert is a way of determining at runtime that some preconditions to the operation are met, and should be used to fail fast and fail hard when you detect that some of your invariants are broken.

Upvotes: 3

michel-slm
michel-slm

Reputation: 9756

You might want to change the range of your index (e.g. indexing from 1 instead of from 0), or represent a higher-dimensional array by a one-dimensional array?

Upvotes: 0

Sadique
Sadique

Reputation: 22821

Well with my limited knowledge on this, i can say, in situations when the classes hold a sequence of elements. Vector/String for example. For assert you may want to check out this site. --LINK--

Upvotes: 1

Related Questions