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

  • SEARCH
  • Home
  • 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 6613177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:11:14+00:00 2026-05-25T20:11:14+00:00

We had an assignment in school implementing a Matrix class that overloads all arithmetic

  • 0

We had an assignment in school implementing a Matrix class that overloads all arithmetic operators. What I did was to (for example) define += as a member function, and then define + as a non-member function that uses += function (both in the same file, but + outside the class).
The school staff did something similar, only they’ve declared ‘+’ as a friend function (and also used the implementation of +=).

Since both implementation work perfectly, I am trying to understand what does a friend function gives me that a non member function does not?
When should I prefer each over the other?

Thanks!
Yotam

  • 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-25T20:11:14+00:00Added an answer on May 25, 2026 at 8:11 pm

    It is preferable not to declare functions friends, if they can be implemented in terms of the class’s public interface (such as operator+ in terms of member operator+=.

    Somehow with operators sometimes people tend to assume that when implemented as free functions they need to be automatically declared friends. E.g you might hear that operator<< cannot be implemented as a member function (because the left-hand operand is an ostream), hence it needs to be a free friend function. In reality it only needs to be a friend if it needs access to private/protected members and member functions.

    (I suspect that might be because overloaded operators, due to their special call syntax, don’t feel like normal functions and seem to have some kind of magical bond with its operands that needs to be expressed in the class definition.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a class assignment that started small, so I had it all
I just had a homework assignment that wanted me to add all the Java
For this assignment I had to create my own string class. I initially wrote
So basically the assignment was we had to create a doubly linked list that's
I recently wrote a class for an assignment in which I had to store
This is a class assignment. I had written some codes which I hope someone
I had an assignment in school last week to implement a function for calculating
I'm writing a program as an assignment for school and I though I had
I had a homework assignment that asked for a function that uses direct recursion
Hi all I was given assignment by the database teacher and i had to

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.