나는 당신이 묻고있는 것을 정말로 말할 수는 없지만, 으로 전달되는 ostream&
에 클래스를 작성한다고 가정합니다. 먼저 T
을 문자열 표현으로 변환하는 방법을 만들어야합니다. 함수 TToString
을 가정합니다.
template<class T>
struct UnknownName
{
T g;
bool operator()(const T&v)
{
if(v==g) {
cout << v;
return true;
}
return false;
}
friend std::ostream& operator<<(std::ostream& os, const T& v) {
return os << TToString(v);
}
};
귀하의 질문을 잘못 해석하면 죄송합니다.
#include <cstdio>
namespace {
struct Foo {
struct Bar {
int i;
};
Foo& operator()(const Bar& b)
{
std::printf("bar, %d\n", b.i);
return *this;
}
// obviously you don't *have* to overload operator()
// to accept multiple types; I only did so to show that it's possible
Foo& operator()(const Foo& f)
{
std::printf("foo\n");
return *this;
}
};
Foo::Bar operator<<(const Foo& f, const Foo& g)
{
Foo::Bar b = { 5 };
return b;
}
}
int main()
{
Foo f, g, h;
f(g << h);
f(g);
}
이 공통 관용구하지 않다가, 적어도 말을하기 :
비회원 함수로 클래스 외부에'operator << '가 있어야합니다. –
'operator <<'는 바이너리 연산자이므로 예제에 인수가 너무 많습니다 ('std :: ostream','const T &'및 리시버 객체'UnknownName'.) 연산자를 호출 하시겠습니까 'UnknownName '객체 또는'const T &'아규먼트에 대해서 –
Dawson