Consider the following minimal example:
#include <iostream>
using namespace std;
class myostream : public ostream {
public:
myostream(ostream const &other) :
ostream(other.rdbuf())
{ }
};
int main() {
cout << "hello world" << endl;
myostream s(cout);
s << "hello world" << endl;
myostream(cout) << "hello world" << endl;
}
The output, both on g++ and on Visual C++, is
hello world
hello world
0x4012a4
The version that writes to a temporary object, myostream(cout)
, appears to prefer the member operator ostream::operator<<(void *)
, instead of the free operator operator<<(ostream &, char *)
. It seems to make a difference whether or not the object has a name.
Why does this happen? And how do I prevent this behaviour?
Edit: Why it happens is now clear from various answers. As to how to prevent this, the following seems appealing:
class myostream : public ostream {
public:
// ...
myostream &operator<<(char const *str) {
std::operator<<(*this, str);
return *this;
}
};
However, this results in all kinds of ambiguities.