Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I overload the [] operators?

I'm creating a class which populates a dictionary as a private member. I want to expose the values of the dictionary without exposing the dictionary itself (in a read-only fashion.) I can easily make a property to expose _dictionary.Keys, but how can I overload [] so that MyClass[key] returns _dictionary[key]?

I think I may need to implement an interface, but I'm not sure which one...

like image 761
dlras2 Avatar asked May 18 '10 16:05

dlras2


People also ask

Can [] operator be overloaded?

In the following example, a class called complx is defined to model complex numbers, and the + (plus) operator is redefined in this class to add two complex numbers. where () is the function call operator and [] is the subscript operator. You cannot overload the following operators: .

What does overloading the [] operator do?

Operator overloading allows C/C++ operators to have user-defined meanings on user-defined types (classes).

What is the correct way to overload operator?

In case of operator overloading all parameters must be of the different type than the class or struct that declares the operator. Method overloading is used to create several methods with the same name that performs similar tasks on similar data types.

How can we overload a function?

You overload a function name f by declaring more than one function with the name f in the same scope. The declarations of f must differ from each other by the types and/or the number of arguments in the argument list.

How do you overload operators in C?

Many operators can be overloaded in a different way: as a member function. Overloading operators using a member function is very similar to overloading operators using a friend function. When overloading an operator using a member function: The overloaded operator must be added as a member function of the left operand.

Why ‘+’ operator is overloaded?

It is achievable because ‘+’ operator is overloaded by int class and str class. You might have noticed that the same built-in operator or function shows different behavior for objects of different classes, this is called Operator Overloading . Attention geek!

What is the best way to overload unary operators?

Unary operators are usually overloaded as member functions as well, since the member version has no parameters. The following rules of thumb can help you determine which form is best for a given situation: If you’re overloading assignment (=), subscript ( []), function call ( ()), or member selection (->), do so as a member function.

What are the different types of operator overloading?

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...


2 Answers

I'm pretty sure this is what you're after:

public TValue this[TKey key] {
    get { return _dictionary[key]; }
}

If you want to implement an interface to indicate to client code that your class can be accessed by an index of type TKey, the closest match (that I'm aware of) is IDictionary<TKey, TValue>.

Unfortunately, IDictionary<TKey, TValue> has a whole bunch of members that violate your read-only requirement, which means you would have to explicitly implement lots of members only to throw a NotImplementedException (or somesuch) when they're called: namely, the setter for this, Add, Clear, and Remove.

Maybe there's a different interface that would be more appropriate for this purpose (something like IReadOnlyDictionary<TKey, TValue>?); I just haven't come across it.

You could also write your own interface, of course, if you intend to have multiple classes that offer functionality similar to this.

like image 86
Dan Tao Avatar answered Oct 05 '22 10:10

Dan Tao


This is called an Indexer. See http://msdn.microsoft.com/en-us/library/2549tw02.aspx

like image 23
Alex Avatar answered Oct 05 '22 10:10

Alex