M
Marcus Kwok
If I have some class Foo, my standard way for printing it is to overload
operator<< as a friend, e.g.,
class Foo {
int i;
public:
Foo() : i(0) { }
friend std:
stream& operator<<(std:
stream& o, const Foo& f);
};
However, I have seen people providing a separate public print() method,
then just calling that from operator<<:
class Foo {
int i;
public:
Foo() : i(0) { }
void print(std:
stream& o);
};
std:
stream& operator<<(std:
stream& o, const Foo& f)
{
f.print(o);
return o;
}
Is there any technical reason why this method should be preferred over
the friend operator<<, assuming that I have complete control over Foo?
operator<< as a friend, e.g.,
class Foo {
int i;
public:
Foo() : i(0) { }
friend std:
};
However, I have seen people providing a separate public print() method,
then just calling that from operator<<:
class Foo {
int i;
public:
Foo() : i(0) { }
void print(std:
};
std:
{
f.print(o);
return o;
}
Is there any technical reason why this method should be preferred over
the friend operator<<, assuming that I have complete control over Foo?