mardi 11 juillet 2023

Operator overloading inside struct

Why i need to make friend keyword if every member of strct is public.

Example:

struct Vector{ int x, y;

friend std::ostream &operator<<(std::ostream &COUT, const Vector &v){
    std::cout << "x: " << v.x << " y: " << v.y;
    return COUT;
}

};

Why can't it be like this:

struct Vector{ int x, y;

std::ostream &operator<<(std::ostream &COUT, const Vector &v){
        std::cout << "x: " << v.x << " y: " << v.y;
    return COUT;
}

};

Aucun commentaire:

Enregistrer un commentaire