Array subscript operator [ ] used for array indexing can also be overloaded in C++. In this post I have added a program in which the array subscript operator [ ] is overloaded. The program illustrates an overloaded array subscript operator.
The program also offers a solution to the C++ puzzle question given below.
Floating Point Number in Array Subscript
Write a C++ program in which the following statement compiles without an error.
cout << a[0.123];
The difficulty in solving this puzzle arises from the fact that array subscript should be an integer.
If you pass a float value to a normal array subscript you will get the following error "Invalid type for array subscript".
One possible solution is to overload the array subscript operator.
There might be some other solutions to this puzzle. If you know any other solution please share them here.
No comments:
Post a Comment