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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:18:06+00:00 2026-06-09T21:18:06+00:00

I created a matrix class: template <typename T> class Matrix { public: Matrix(size_t n_rows,

  • 0

I created a matrix class:

template <typename T>
class Matrix
{
public:
    Matrix(size_t n_rows, size_t n_cols);
    Matrix(size_t n_rows, size_t n_cols, const T& value);

    void fill(const T& value);
    size_t n_rows() const;
    size_t n_cols() const;

    void print(std::ostream& out) const;

    T& operator()(size_t row_index, size_t col_index);
    T operator()(size_t row_index, size_t col_index) const;
    bool operator==(const Matrix<T>& matrix) const;
    bool operator!=(const Matrix<T>& matrix) const;
    Matrix<T>& operator+=(const Matrix<T>& matrix);
    Matrix<T>& operator-=(const Matrix<T>& matrix);
    Matrix<T> operator+(const Matrix<T>& matrix) const;
    Matrix<T> operator-(const Matrix<T>& matrix) const;
    Matrix<T>& operator*=(const T& value);
    Matrix<T>& operator*=(const Matrix<T>& matrix);
    Matrix<T> operator*(const Matrix<T>& matrix) const;

private:
    size_t rows;
    size_t cols;
    std::vector<T> data;
};

Now I want to enable operations between matrix of different types, for example:

Matrix<int> matrix_i(3,3,1); // 3x3 matrix filled with 1
Matrix<double> matrix_d(3,3,1.1); // 3x3 matrix filled with 1.1

std::cout << matrix_i * matrix_d << std::endl;

I thought to do like this (is the right way?):

template<typename T> // Type of the class instantiation
template<typename S>
Matrix<T> operator*(const Matrix<S>& matrix)
{
   // Code
}

I think this will work fine if I multiply a double matrix with an integer matrix: I will obtain a new double matrix. The problem is that if I multiply an integer matrix with a double matrix I will lose some information, because the matrix I obtain will be an integer matrix… Right? How can I fix this behave?

std::cout << matrix_d * matrix_i << std::endl; // Works: I obtain a 3x3 matrix full of 1.1
std::cout << matrix_i * matrix_d << std::endl; // Doesn't work: I obtain a 3x3 matrix full of 1 instead of 1.1
  • 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-09T21:18:08+00:00Added an answer on June 9, 2026 at 9:18 pm

    To do what you want, you will need to offer an operator* that returns a Matrix<X> where X is the type with the largest range/greatest precision.

    If you have a C++11 compiler at hand, you could use:

    template <typename T, typename U>
    auto operator*( Matrix<T> const & lhs, Matrix<U> const & rhs) 
         -> Matrix< delctype( lhs(0,0) * rhs(0,0) ) >
    {
       Matrix< delctype( lhs(0,0) * rhs(0,0) ) > result( lhs );
       result *= rhs;
       return result;
    }
    

    Assuming that you have operator*= implemented as a template that allows multiplications with other instantiations of Matrix<T> and that you have a conversion constructor.

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

Sidebar

Related Questions

I created a matrix class: template <typename T> class Matrix { static_assert(std::is_arithmetic<T>::value,); public: Matrix(size_t
I created the following Matrix class: template <typename T> class Matrix { static_assert(std::is_arithmetic<T>::value,); public:
I created a Matrix class, using static_assert(std::is_arithmetic<T>::value,); to check if the template type is
I have a class template<size_t N, size_t M> class Matrix { // .... };
I have a template class which implements function: template<typename T> class Matrix { ...
I recently created a generic Matrix<T> class that acts as a wrapper around a
I am coding a matrix template class and the problem I am having is
I am working on an exercise in operator overloading. I've created a matrix class
So I have a generic Matrix class that I created which has the following
I created a 2-D matrix using double pointer like that: int** pt; pt =

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.