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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:37:32+00:00 2026-05-13T23:37:32+00:00

I suppose the naive implementation of a + operator for matrices (2D for instance)

  • 0

I suppose the naive implementation of a + operator for matrices (2D for instance)
in C++ would be:

class Matrix {

  Matrix operator+ (const Matrix & other) const {
      Matrix result;
      // fill result with *this.data plus other.data
      return result;
  }
}

so we could use it like

Matrix a;
Matrix b;
Matrix c;

c = a + b;

Right?

But if matrices are big this is not efficient as we are doing one not-necessary copy (return result).

Therefore, If we wan’t to be efficient we have to forget the clean call:

c = a + b;

Right?

What would you suggest / prefer ?
Thanks.

  • 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-13T23:37:33+00:00Added an answer on May 13, 2026 at 11:37 pm

    The C++ standard gives permission for the compiler to elide the unnecessary copy in this case (it’s called the “named return value optimization”, usually abbreviated to NRVO). There’s a matching “RVO” for when you return a temporary instead of a named variable.

    Nearly all reasonably recent C++ compilers implement both NRVO and RVO, so generally speaking you can ignore the fact that this construct wouldn’t otherwise be particularly efficient.

    Edit: I was, of course, talking about the copy involved in returning the new matrix holding the result of the addition. You probably do want to either pass the input by const reference:

    Matrix operator+(Matrix const &other) const { 
        Matrix result;
        // ...
        return result;
    }
    

    …or else, pass by value, but return the passed value:

    Matrix operator+(Matrix other) const { 
        other += *this;
        return other;
    }
    

    Note that this depends on commutativity though (i.e., it’s really doing b+a instead of a+b) so while it’s fine for addition, it won’t work for some other operations.

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

Sidebar

Related Questions

This might sound naive, but... class Widget { public int Foo { get; set;
Suppose your git history looks like this: 1 2 3 4 5 1–5 are
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose that I have the following mapping with a formula property: <class name=Planet table=planets>
Suppose we have some class A that implements some interface I I i =
I have a naive question about OOP design patterns. This is over-architecture, but I
Let's suppose i have the following piece of code: $qb = $this->em->createQueryBuilder(); $qb->add('select', 'a')
Suppose after N occurrences, there are P times that an event happens. The naive
This is probably a naive question about XmlReader , but I haven't turned up

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.