0

I know that this works:

struct vtx
{
    long  operator[](long line)
    {
        return line;
    }
};

but why can´t I instead do something like this to emulate a two column access? Is there any way to do it?

struct vtx
{
    long  operator[](long line, long column)
    {
        return line + column; //resolved later
    }
};
ChrCury78
  • 427
  • 3
  • 8

1 Answers1

1

operator[] is defined to only accept 1 parameter. It is not possible to accept 2 parameters, but you can accept a tuple instead:

struct vtx
{
    long  operator[](std::tuple<long, long> loc)
    {
        return std::get<0>(loc) + std::get<1>(loc);
    }
};

// used as foo[std::make_tuple(0, 1)]
Tim Dumol
  • 584
  • 4
  • 14
  • 3
    You'd probably use `foo[{0, 1}]` instead. But then you might as well use `operator()` in the first place, which can actually take multiple arguments like `foo(1, 2)`. – Max Langhof Nov 29 '19 at 11:56
  • 1
    @MaxLanghof Wow, I didn't know curly braces for tuples worked. Thanks! – Tim Dumol Nov 29 '19 at 13:01