1

From this sample code from Boost official site:

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    for (unsigned i = 0; i < m.size1 (); ++ i)
        for (unsigned j = 0; j < m.size2 (); ++ j)
            m (i, j) = 3 * i + j;
    std::cout << m << std::endl;
}

I am confused with m (i, j) = 3 * i + j; because m is a object and the only case that combines class and argument together is constructor function, but there, it is obviously not.

I am a beginner of C++. However, being different from Ruby there is few trick in C++.

In order to have a deep discovery toward C++, is there any who can give me a explanation in principle about that?

Frederik.L
  • 5,522
  • 2
  • 29
  • 41
pah8J
  • 807
  • 9
  • 15
  • No, `m` is not a class, it's a variable. – user202729 Apr 08 '18 at 03:17
  • In c++ you can define your own operators, one of the most popular being `operator[]`, but `operator()` is also possible. Check this: https://www.boost.org/doc/libs/1_57_0/boost/numeric/ublas/matrix.hpp and search for `const_reference operator () (size_type i, size_type j)` – Frederik.L Apr 08 '18 at 03:18
  • Also, all lvalues can be assigned. | You need to learn more about C++ before asking questions to [so] otherwise such questions would be considered unclear. – user202729 Apr 08 '18 at 03:18
  • @user202729, I mean object, it is my written fault. – pah8J Apr 08 '18 at 05:02
  • @Frederik.L, thanks a lot. In my imagination, () is not a operator, because function call and new object. Now I have new understanding to C++. – pah8J Apr 08 '18 at 05:06
  • @MartinS.Victory You are welcome. Since it seems to answer the question, I will post it as an answer. – Frederik.L Apr 08 '18 at 05:33
  • You need to learn more about C++ [operator overloading](http://en.cppreference.com/w/cpp/language/operators) and probably [object](http://en.cppreference.com/w/cpp/language/object). – user202729 Apr 08 '18 at 05:45

1 Answers1

2

In C++, you can define your own operators (and override them if you need to). One popular operator for accessors is []. However, () is possible as well for a custom operator.

If you take look at the source code of matrix.hpp from Boost, in which the matrix object is defined, there is indeed an operator ().

/** Access a matrix element. Here we return a const reference
 * \param i the first coordinate of the element. By default it's the row
 * \param j the second coordinate of the element. By default it's the column
 * \return a const reference to the element
 */
    BOOST_UBLAS_INLINE
    const_reference operator () (size_type i, size_type j) const {
        return data () [layout_type::element (i, size1_, j, size2_)];
    }

and

/** Access a matrix element. Here we return a reference
 * \param i the first coordinate of the element. By default it's the row
 * \param j the second coordinate of the element. By default it's the column
 * \return a reference to the element
 */
    BOOST_UBLAS_INLINE
    reference operator () (size_type i, size_type j) {
        return at_element (i, j);
    }

    // Element assignment

The lower level implementation of Boost mechanics might be a little complex to understand at the first look, but what allows it to have a syntax like this is the presence of operator () in the definition.

You can check simpler examples about operators, for example there (on cppreference).

Frederik.L
  • 5,522
  • 2
  • 29
  • 41