重载双括号函数并在同一类中使用它

Overloading the Double Parentheses function and using it inside the same class?

本文关键字:一类 函数 重载      更新时间:2023-10-16

我重载双括号函数并将其用于 C++ 中的矩阵的同一类中?我相信我正确地超载了它,但我不知道如何从内部调用它。

我假设你的双括号函数是operator(),那么你可以通过两种不同的方式从其他成员函数内部调用它:

    unsigned operator()(unsigned i) const
    {
        if(i == 0) return 1;
        return operator()(i-1);
    }

    unsigned operator()(unsigned i) const
    {
        if(i == 0) return 1;
        return (*this)(i-1);
    }