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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:59:18+00:00 2026-05-11T02:59:18+00:00

I’m writing this question with reference to this one which I wrote yesterday. After

  • 0

I’m writing this question with reference to this one which I wrote yesterday. After a little documentation, it seems clear to me that what I wanted to do (and what I believed to be possible) is nearly impossible if not impossible at all. There are several ways to implement it, and since I’m not an experienced programmer, I ask you which choice would you take. I explain again my problem, but now I have some solutions to explore.

What I need

I have a Matrix class, and I want to implement multiplication between matrices so that the class usage is very intuitive:

Matrix a(5,2); a(4,1) = 6 ; a(3,1) = 9.4 ;            ...                   // And so on ...  Matrix b(2,9); b(0,2) = 3; ...                   // And so on ...  // After a while Matrix i = a * b; 

What I had yesterday

At the moment I overloaded the two operators operator* and operator= and until yesterday night the were defined in this way:

Matrix& operator*(Matrix& m); Matrix& operator=(Matrix& m); 

The operator* instantiates a new Matrix object (Matrix return = new Matrix(...)) on the heap, set the values and then just:

return *result; 

What I have today

After the discussion I decided to implement it in a ‘different way’ to avoid the user to be bothered bother by pointers of any type and to keep the usage unchanged. The ‘different way’ is to pass the returning value of operator* by value:

Matrix operator*(Matrix& m); Matrix& operator=(Matrix& m); 

The operator* instantiates return on the stack, set the values and then return the object.

There is a problem with this approach: it doesn’t work. The operator= expects a Matrix& and operator* returns a Matrix. Moreover this approach doesn’t look so good to me for another reason: I’m dealing with matrices, that can be very large and the aims of this library were to be 1) good enough for my project 2) fast, so probably passing by value should not be an option.

Which solutions I have explored

Well, following the suggestions in the previous discussion I read some stuff about smart pointers, they look great but I can’t still figure out how to solve my problem with them. They deal with memory freeing and pointer copying, but I’m basicly using references, so they don’t look the right choice for me. But I may be wrong.

Maybe the only solution is to pass by value, maybe I can’t get both efficiency and a good interface. But again, you’re the expert, and I would like to know your opinion.

  • 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. 2026-05-11T02:59:18+00:00Added an answer on May 11, 2026 at 2:59 am

    The problem you are having is that the expression a * b creates a temporary object, and in C++, a temporary is not allowed to bind to a non-constant reference, which is what your Matrix& operator=(Matrix& m) takes. If you change it to:

    Matrix& operator=(Matrix const& m); 

    The code should now compile. As well as the obvious benefit of producing compilable code :), adding the const also communicates to your callers that you will not be modifying the argument m, which may be helpful information.

    You should also do the same for your operator*():

    Matrix operator*(Matrix const& m) const; 

    [EDIT: The additional const at the end indicates that the method promises not to alter *this, the object on the left-hand side of the multiplication, either. This is necessary to cope with expressions such as a * b * c — the subexpression a * b creates a temporary and won’t bind without the const at the end. Thanks to Greg Rogers for pointing this out in the comments.]

    P.S. The reason why C++ does not allow a temporary to bind to a non-constant reference is because temporaries exist (as the name suggests) for only a very short time, and in most cases, it would be a mistake to attempt to modify them.

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

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer What do you want to do with it? There are… May 11, 2026 at 2:47 pm
  • added an answer Found the following on Experts Exchange: select ora_database_name from dual;… May 11, 2026 at 2:47 pm
  • added an answer Use ELMAH - it can capture ajax errors. May 11, 2026 at 2:47 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.