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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:53:09+00:00 2026-06-17T19:53:09+00:00

I have a Matrix class and it has overloaded * operators for scalar and

  • 0

I have a Matrix class and it has overloaded * operators for scalar and matrix multiplications.

template <class T> class Matrix
{
    public:
        // ...
        Matrix operator*(T scalar) const;
        // ...
}

// ...

template <class T>
Matrix<T> Matrix<T>::operator*(T RightScalar) const
{
    Matrix<T> ResultMatrix(m_unRowSize, m_unColSize);
    for (uint64_t i=0; i<m_unRowSize; i++)
    {
        for (uint64_t j=0; j<m_unColSize; j++)
        {
            ResultMatrix(i, j) = TheMatrix[m_unColSize * i + j] * RightScalar;
        }
    }
    return ResultMatrix;
}

// ...

I can multiply a matrix object with a scalar from right side without any problem:

Matrix<double> X(3, 3, /* ... */);  // Define a 3x3 matrix and initialize its contents
Matrix<double> Y;                   // Define an output matrix
Y = X * 10.0;                       // Do the linear operation

But, how do I multiply it from left side same way?

Matrix<double> X(3, 3, /* ... */);
Matrix<double> Y;
Y = 10.0 * X;

In arithmetic, it is a common notation to write constants on the left side when doing multiplication. I would like to obey this rule to make my code more readable.

Is it possible to implement this in C++?
If it is possible, how do I modify the class method in my code?

  • 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-17T19:53:10+00:00Added an answer on June 17, 2026 at 7:53 pm

    Member functions are matched by their left-hand-side argument which is the this-pointer. Since native types can’t have member functions, you have to add right-multiplication with user-defined types through non-member functions (and also for other types you don’t have write-access to).

    template<typename T>
    Matrix<T> operator*(T const& scalar, Matrix<T> rhs)
    {
        // scalar multiplication is commutative: s M = M s
        return rhs *= scalar; // calls rhs.operator*=(scalar);
    }
    

    NOTE: I wrote the above non-member operator* implemented in terms of a member operator*=. It is recommended to write all multiplications as non-member functions, and use a member operator*= to implement these multiplications with a lhs Matrix element.

    This will a) keep the class interface minimal, and b) prevent hidden conversions. E.g. you could have a Matrix class that is implicitly convertible to scalar if the dimensions are 1×1, and these conversions could silently happen if you don’t provide a separate overload that is a direct match.

    template<typename T>
    Matrix<T> operator*(Matrix<T> lhs, T const& scalar)
    {
        return lhs *= scalar; // calls lhs.operator*=(scalar);
    }
    
    template<typename T>
    Matrix<T> operator*(Matrix<T> lhs, Matrix<T> const& rhs)
    {
        return lhs *= rhs; // calls lhs.operator*=(rhs);
    }
    

    Notice on how the lhs Matrix is a copy and not a reference. This allows the compiler to make optimizations such as copy elision / move semantics. Also note that the return type of these operators is Matrix<T> and not const Matrix<T> which was recommended in some old C++ books, but which prevents move semantics in C++11.

    // class member 
    template<typename T>
    Matrix<T>& Matrix<T>::operator*=(Matrix<T> const& rhs)
    {
        // your implementation
        return *this;
    }
    
    // class member 
    template<typename T>
    Matrix<T>& Matrix<T>::operator*=(T const& scalar)
    {
        // your implementation
        return *this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class template that has a boost matrix as a private member
I have the following class: template <typename T> class matrix { private: int _n;
We have two classes: template<typename T, typename Size, typename Stack, typename Sparse> class Matrix
I have a simple matrix class which has a 2d integer pointer field in
So I have a generic Matrix class that I created which has the following
I have a matrix class with the size determined by template parameters. template <unsigned
I have a matrix class like below: template <size_t M, size_t N, typename T>
Given: class Base: { public: ... Base operator+( const Base& other ); Base& scale(
I am attempting to create an overloaded operator for a matrix class that I
I have a code base, in which for Matrix class, these two definitions are

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.