加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 使用内联重载运算符

发布时间:2020-12-16 09:20:42 所属栏目:百科 来源:网络整理
导读:我重载了运算符用于矩阵输出. std::ostream operator (std::ostream os,const Tic b){ for (int i = 0; i b.rows; i++) { for (int j = 0; j b.cols; j++) os std::setw(5) b.board[i][j] " "; os 'n'; } return os;} 另外,我创建了一个小型打印功能. inlin
我重载了运算符<<用于矩阵输出.

std::ostream& operator << (std::ostream& os,const Tic& b)
{
    for (int i = 0; i < b.rows; i++)
    {
        for (int j = 0; j < b.cols; j++)
            os << std::setw(5) << b.board[i][j] << " ";
        os << 'n';
    }
    return os;
}

另外,我创建了一个小型打印功能.

inline void print_matrix (const Matrix& _obj)
{
    cout << _obj;
}

Can I use inline for print_matrix function?

Will inline be used for overloaded operator,or does the compiler
apply this only for cout and only then will call << as another
function?

解决方法

如果我正确理解您的问题,您想知道是否内联

inline void print_matrix (const Matrix& _obj)
{
    cout << _obj;
}

也导致调用<<由编译器内联. 问题是:你的前提是错误的.很久以前就引入了inline来控制编译器内联的函数.然而,随着时间的推移,事实证明编译器在决定内联比人类更好的方面要好得多.因此,内联只是对编译器的一个暗示,它的唯一实际用例仍然是告诉链接器,当你在头文件中定义的函数使用内联时,它会找到函数的多个定义.有关详细信息,另请参见here.

TL; DR:上面的内联函数甚至没有告诉你print_matrix是否内联.如果你想知道编译器真正内联的内容,我建议你使用这个工具:https://godbolt.org/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读