8

演算子を操作するには、クラス内にどの関数や演算子を実装する必要があるのか​​知りたいのですがboost::format %

例えば:

class A
{
    int n;
    // <-- What additional operator/s and/or function/s must be provided?
}

A a;
boost::format f("%1%");
f % a;

私はPretty-printC++ STLコンテナーautoを研究してきましたが、これは私の質問に何らかの形で関連していますが、これにより、関連する問題や他のさまざまな言語機能に関するレビューと学習に何日も費やされました。私はこの調査のすべてをまだ終えていません。

誰かがこの特定の質問に答えることができますか?

4

1 に答える 1

4

operator<<適切な出力演算子( )を定義する必要があります。

#include <boost/format.hpp>
#include <iostream>

struct A
{
    int n;
    
    A() : n() {}
    
    friend std::ostream &operator<<(std::ostream &oss, const A &a) {
        oss << "[A]: " << a.n;
        return oss;
    }
};

int main() {
    A a;
    boost::format f("%1%");
    std::cout << f % a << std::endl;
}
于 2012-11-10T22:23:53.310 に答える