I have a custom class that I am trying to print, I have it working how I am unsure why it is working and I was hoping someone could explain it. Taking the idea from this question I have created a small example,
class myclass {
public:
int i = 5;
};
and manages to print it by using,
std::ostream &operator<<(std::ostream &os, myclass const &m) {
return os << m.i;
}
myclass x;
std::cout << x;
However, other override operators have to include the class namespace such as,
myclass myclass::operator+(const myclass & c) {
string temp;
temp.i = i + c.i;
return temp;
}
Notice here how I needed the namespace for the class myclass::operator+
?
Why was this not required to work for the <<
operator? How is it associated with myclass
?
i.e I expected something like this,
v----this added
std::ostream myclass::operator<<(std::ostream &os, myclass const &m) {
return os << m.i;
}