I wanted to print some properties directly to cout. Luckily I found a solution for static calls like this:
// somewhere:
ostream & Foo(ostream &ostr){
ostr << "Foo";
return ostr;
}
and another static call where it can be called directly like that:
// somoewhere else:
void doSomething(){
cout << 123 << Foo << endl;
}
This works fine and prints simply "123Foo". But I want to use this for an object I have, so I could print its properties. In my class A I have a property b of the class B. There the function is implemented similar to the static one before:
ostream & Foo(ostream &ostr) {
ostr << "my foo is 123";
return ostr;
}
but it doesn't compile. I also tried to specify the namespace like
ostream & B::Foo(ostream &ostr) { ... }
and assumed I could just call the object's function like this:
cout << 123 << b->Foo << endl;
but it always tells me there is an error: "Reference to non-static member function must be called".
I just can't get this done. What am I doing wrong? Is this even possible?
NOTE: I don't want to overload the << operator or anything. I don't want a "toString()" method or anything similar but would like to understand why this instance method can not be called like anywhere else. As a Java developer I would have guessed something like "My object " + b.foo() would be possible. But it seems, it is not.