Home C++ operator overloading <double> - <Matrix>
Reply: 0

C++ operator overloading <double> - <Matrix>

user1079 Published in April 27, 2018, 6:52 am

This question already has an answer here:

  • What are the basic rules and idioms for operator overloading? 7 answers

Let's suppose that we want to subtract some value from a given matrix. How could/should I overload that operator.


Matrix<double> m(10, 5);

auto r = 1.0 - m; //error: return type specified for 'operator double'


template <typename T>
class Matrix {
  Matrix operator double(T val) {
    Matrix tmp(rows, cols);

    for (unsigned int i = 0; i < rows; i++)
      for (unsigned int j = 0; j < cols; j++) {
        const unsigned int idx = VecToIdx({i, j});
        tmp[idx] = val - this[idx];

    return tmp;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.319204 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO