U
Urs Thuermann
I tried overloading the operator<<() for ostream as a static member
function in a class, but the compiler (gcc) complained that it should
be non-static or non-member. Why is this not possible? Since I want
to pass a private type of the class as argument, this is a problem.
What I tried to do is something like this:
#include <iostream>
using namespace std;
class Foo {
public:
void foo() {
#if 0
cout << "yadda yadda " << b << endl;
#endif
}
private:
struct Bar {
public:
#if 1
static ostream &operator<<(ostream &os, const Bar &b)
{
b.print(os);
return os;
}
#endif
private:
int bar;
void print(ostream &os) const { os << bar; }
};
Bar b;
};
I cannot make the operator<< a non-member function since its parameter
type Bar is private and I'd prefer to leave it so. How should I solve
this?
When I replace the "operator<<" by some other function name the code
compiles but I still cannot use << like in foo().
urs
function in a class, but the compiler (gcc) complained that it should
be non-static or non-member. Why is this not possible? Since I want
to pass a private type of the class as argument, this is a problem.
What I tried to do is something like this:
#include <iostream>
using namespace std;
class Foo {
public:
void foo() {
#if 0
cout << "yadda yadda " << b << endl;
#endif
}
private:
struct Bar {
public:
#if 1
static ostream &operator<<(ostream &os, const Bar &b)
{
b.print(os);
return os;
}
#endif
private:
int bar;
void print(ostream &os) const { os << bar; }
};
Bar b;
};
I cannot make the operator<< a non-member function since its parameter
type Bar is private and I'd prefer to leave it so. How should I solve
this?
When I replace the "operator<<" by some other function name the code
compiles but I still cannot use << like in foo().
urs