Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ How to not use friend declaration when equipping a class with `operator<<`

#include <iostream>

class Point {
   private:
    double m_x{};
    double m_y{};
    double m_z{};

   public:
    Point(double x = 0.0, double y = 0.0, double z = 0.0)
        : m_x{x}, m_y{y}, m_z{z} {}

};

std::ostream& operator<<(std::ostream& out, const Point& point) {
    out << "Point";
    // Use public interface of point here.
    return out;
}

int main() {
    const Point point1{2.0, 3.0, 4.0};

    std::cout << point1 << '
';

    return 0;
}
 
PREVIOUS NEXT
Tagged: #How #friend #declaration #equipping #class
ADD COMMENT
Topic
Name
5+7 =