How can I “override” [] to accept two arguments in C++?

后端 未结 6 1187
[愿得一人]
[愿得一人] 2020-12-30 09:48

I am trying to create a bit-vector class in C++ to model some hardware. In most HDLs (hardware description langauges) that I know, specific bits are referenced like this:

6条回答
  •  攒了一身酷
    2020-12-30 10:27

    Is there any way to tell operator[] to accept two arguments?

    No.

    There's two common alternatives. One is to overload operator() instead. You then have to invoke it using the () syntax.

    The other one is to have operator[] return a proxy object for which operator[] is overloaded, too. This can be invoked like multi-dimensional arrays in C, with several [][] in series.

提交回复
热议问题