Overloading the << operator.


Outside Class definition.

std::ostream& operator<<(std::ostream& os, const QualifierEnum qe) {
    switch(qe) {
        case QualifierEnum::Pass: os << ""; break;
        case QualifierEnum::Fail: os << "-"; break;
        case QualifierEnum::SoftFail: os << "~"; break;
        case QualifierEnum::Neutral: os << "?"; break;
    }
    return os;
}

Within a Class definition.

Header File

class Sample {
  
  private:
    ...
  public:
    friend std::ostream& operator<<(std::ostream& os, Qualifier const &qualifier);
}

Here we use friend to prevent the compiler from complaining about the number of arguments.

Source File

std::ostream& operator<<(std::ostream& os, Qualifier const& qualifier) {
    if (qualifier.is_pass()) return os << "";
    return os << static_cast<char>(qualifier._value);
}

See also