Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6209783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:02:50+00:00 2026-05-24T06:02:50+00:00

My code was reviewed on: https://codereview.stackexchange.com/questions/3754/c-script-could-i-get-feed-back/3755#3755 The following was used: class Point { public:

  • 0

My code was reviewed on:
https://codereview.stackexchange.com/questions/3754/c-script-could-i-get-feed-back/3755#3755

The following was used:

class Point
{
    public:
    float   distance(Point const& rhs) const
    {
        float dx    = x - rhs.x;
        float dy    = y - rhs.y;

        return sqrt( dx * dx + dy * dy);
    }
    private:
        float   x;
        float   y;
        friend std::istream& operator>>(std::istream& stream, Point& point)
        {
            return stream >> point.x >> point.y;
        }
        friend std::ostream& operator<<(std::ostream& stream, Point const& point)
        {
            return stream << point.x << " " << point.y << " ";
        }
};

by another member. I don’t understand what friend functions are doing. Is there another way to do this without making them friend functions? And how can the client access them when they are private using the following? Could someone expound on what exactly is being returned?

int main()
{
    std::ifstream       data("Plop");

    // Trying to find the closest point to this.
    Point   first;
    data >> first;

    // The next point is the closest until we find a better one
    Point   closest;
    data >> closest;

    float   bestDistance = first.distance(closest);

    Point   next;
    while(data >> next)
    {
        float nextDistance  = first.distance(next);
        if (nextDistance < bestDistance)
        {
            bestDistance    = nextDistance;
            closest         = next;
        }
    }

    std::cout << "First(" << first << ") Closest(" << closest << ")\n";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:02:51+00:00Added an answer on May 24, 2026 at 6:02 am

    And how can the client access them when they are private using the following?

    Yes. Since friend functions are not member of the class, it doesn’t matter where you define them or declare them. Any can use them. The access rules don’t apply on them.

    Could someone expound on what exactly is being returned?

    operator>>() returns std::istream& which is reference to input stream.
    And operator<<() returns std::ostream& which is reference to output stream.

    Is there another way to do this without making them friend functions?

    Yes. There is a way. You can add two member functions input and output to the public section of the class, which will do what friend functions are doing now, and you can make operator<< and operator>> non-friend functions as follows:

    class Point
    {
        public:
        //....
        std::istream& input(std::istream& stream)
        {
           return stream >> point.x >> point.y;
        }
        std::ostream& output(std::ostream& stream) const
        {
           return stream << point.x << " " << point.y << " ";
        }
        //...
    };
    
    std::istream& operator>>(std::istream& stream, Point& point)
    {
      return point.input(stream);
    }
    std::ostream& operator<<(std::ostream& stream, Point const& point)
    {
      return point.output(stream);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I dont get it... I reviewed my code hundred times and I dont get
I tried to control email address and reviewer's name with the following code but
Code: <html xmlns=http://www.w3.org/1999/xhtml> <head> <title>Unusual Array Lengths!</title> <script type=text/javascript> var arrayList = new Array();
Code that is untestable really annoys me. The following things make oo-code untestable: global
Alright, probably not the best title, but meh. I have the following code inside
I recently had my code reviewed by a senior developer in my company. He
< I have reviewed the related questions shown by SO before posting this >
I have been working on some code that is similar to the following: typedef
I've reviewed questions How to use include directive correctly and C++ #include semantics and
I've seen a number of 'code metrics' related questions on SO lately, and have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.