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

后端 未结 6 1165
[愿得一人]
[愿得一人] 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:17

    a simple struct with two members as a parameter to operator[]...

    struct pos
    {
      int lsb;
      int msb;
    };
    
    pos foo={1,2};
    
    my_vector[foo];
    

    or in the new standard, I believe you can simply do:

    my_vector[pos{1,2}]
    

提交回复
热议问题