Overload [] for entry In C++?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
18 views
Is it possible to do so by using the indexer could assign the value, not only to read?
MyClass a[5] = {1,2,2}
by | 18 views

2 Answers

0 like 0 dislike
#include template class TestArray { public: T& operator[](size_t i) { return data[i]; } private: T data[N]; }; int main() { TestArray arr; arr[0] = 99; arr[1] = -1; std::cout << arr[0] << std::endl; std::cout << arr[1] << std::endl; return 0; }
by
0 like 0 dislike
Nothing to overload, the appropriate constructor
\rhttps://ideone.com/q0wcpy
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
asked Apr 9, 2019 by Kalombyr
0 like 0 dislike
2 answers
asked Apr 24, 2019 by kytcenochka
0 like 0 dislike
2 answers
110,608 questions
257,187 answers
0 comments
40,796 users