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 1088745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:06:56+00:00 2026-05-16T23:06:56+00:00

I have 2 classes which represent a matrix: 1. RegularMatrix – O(n^2) representation 2.

  • 0

I have 2 classes which represent a matrix:
1. RegularMatrix – O(n^2) representation
2. SparseMatrix – a matrix that is represented as linked list (without zeros).

lets say i have:

RegularMatrix a;
SparseMatrix b;

i want to be able to do:

a+b;

and also:

b+a;

so i’m overloading the + operator. My question is, since I want the addition to be commutative (a+b = b+a), do i need to implement 2 overloadings, one for each case?

RegularMatrix operator+(const RegualarMatrix &, const SparseMatrix &);
RegularMatrix operator+(const SparseMatrix & ,const RegualarMatrix &);

or is there a general form which the compiler decides by itself?

Thank you

  • 1 1 Answer
  • 4 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-16T23:06:57+00:00Added an answer on May 16, 2026 at 11:06 pm

    Yes you need both versions. But you can forward the one to the other, if the operation really is commutative

    RegularMatrix operator+(const SparseMatrix &a, const RegualarMatrix &b) {
        return b + a;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of classes that represent various computer components, each of which
I have a few classes which have a property which is unique to that
I have two classes which represent a special kind of numeric value. Let's call
I have a set of classes that represent the storage of a font format
I have 5 classes which represent a row of a grid of data. All
I have my own classes that represent image data. They have various underlying structure
I have classes which have automatic properties only like public customerName {get; set;}. They
I have four classes which share some arrangement of four properties. I have currently
I have some classes which extends a superclass, and in the JSP I want
I have various classes which all contain address details, i.e. AddressLine1, AddressLine2, AddressLine3, Suburb,

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.