カスタムタイプのオーバーロードstd :: coutおよびqDebug()

struct ceshi
{
    int frist;
    int second;
    ceshi(int one = 0,int two = 0):frist{one},second{two}
    {
    }
    friend std::ostream& operator<<(std::ostream&,const ceshi&);
    friend QDebug operator<<(QDebug dbg, const ceshi&);
};

std::ostream & operator << (std::ostream & os,const ceshi & c)
{
    return os << "std::cout 输出 ceshi{ frist = " << c.frist << ",second = " << c.second << "}";
}

QDebug operator<<(QDebug dbg, const ceshi & c)
{
    dbg.nospace() << "debug 输出 ceshi{ frist = " << c.frist <<",second = "<< c.second << "}";
    return dbg;
}

#define debug qDebug()<<
int main(int argc, char *argv[])
{
    ceshi c(1,2);
    std::cout << c;
    debug c;
}

std :: cout中国語の文字化け...

Qtには、qDebugのオーバーロードを具体的に紹介する「カスタムタイプの例」のデモがあります。

おすすめ

転載: blog.csdn.net/kenfan1647/article/details/113793648