小编Jam*_*esM的帖子

有没有办法提取多个相似函数的外循环?

示例:我想从这些运算符函数中提取嵌套的 for 循环,这些运算符函数除了一行之外都是相同的。

// Add two matrices
Matrix& operator+=(const Matrix& other)
{
    for (int i = 0; i < this->m_rows; i++)
    {
        for (int j = 0; j < this->m_cols; j++)
        {
            (*this)(i, j) = (*this)(i, j) + other(i, j); // Only difference
        }
    }
    return *this;
}

// Subtract two matrices
Matrix& operator-=(const Matrix& other)
{   
    for (int i = 0; i < this->m_rows; i++)
    {
        for (int j = 0; j < this->m_cols; j++)
        {
            (*this)(i, j) = …
Run Code Online (Sandbox Code Playgroud)

c++ code-duplication

6
推荐指数
1
解决办法
138
查看次数

标签 统计

c++ ×1

code-duplication ×1