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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:22:04+00:00 2026-06-15T00:22:04+00:00

class matrix { int n; double **a; //the matrix public: matrix(int); ~matrix(); int getN();

  • 0
class matrix
{
    int n;
    double **a;  //the "matrix"
public:
    matrix(int);
    ~matrix();
    int getN();
    matrix& operator=(matrix&);
    double& operator()(int,int);
    friend matrix& operator+(matrix,matrix);
    friend matrix& operator-(matrix,matrix);
    friend matrix& operator*(matrix,matrix);
    friend ostream& operator<<(ostream &,const matrix &);
};

matrix& operator+(matrix A,matrix B)
{
    int i,j,n=A.getN();
    assert(A.getN()==B.getN());
    matrix *C=new matrix(A.getN());
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            (*C)(i,j)=A(i,j)+B(i,j);
        }
    }
    return *C;
}

Is this the correct way to overload arithmetic operators?

Are there memory leaks in my code?

The constructor allocates memory in heap, first for an array of double pointers, then for each pointer an array of double.

  • 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-06-15T00:22:06+00:00Added an answer on June 15, 2026 at 12:22 am

    You should be returning the new matrix by value, and passing (at leas one of) the arguments by const reference:

    matrix operator+(const matrix& A, const matrix& B);
    

    This means that you should not allocate it dynamically inside the body of the operator.

    If you only call public member methods or member operators, then there is no need to declare the non-member operators as friend.

    Also note that is is common practice to implement +=, *= etc. as member operators, and then implement the non-members in terms of those:

    matrix operator+(matrix A, const matrix& B)
    {
      return A+=B;
    }
    

    As an aside, you have to check that the dimensions of the matrices are correct. With your design it is only possible to do this at run-time. Another approach is to enforce dimensional correctness at compile time by making the matrices class templates:

    template <typename T, size_t ROWS, size_t COLS> matrix;
    

    The trade-off is that matrices of different dimensions are different types.

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

Sidebar

Related Questions

I am trying to convert this C# code to F#: double[,] matrix; public Matrix(int
This is my template matrix class: template<typename T> class Matrix { public: .... Matrix<T>
public class DoubleMatrix { private double[][] doubMatrix; public DoubleMatrix(int row, int col) { if(row
I wrote a small sparse matrix class with the member: std::map<int,std::map<int,double> > sm; The
I'm trying to convert the following C# into F#: public class Matrix { double[,]
I have the following class: template <typename T> class matrix { private: int _n;
I'm declaring an instance of a class like so: Matrix m; This appears to
public class Homework2 { public static void main(String[] args){ int num1 = (int) (Math.random()*(10-3+1)+3);
I created a matrix class: template <typename T> class Matrix { public: Matrix(size_t n_rows,
i have a class Matrix and I use the this pointer in the application

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.