This forum has been archived. All content is frozen. Please use KDE Discuss instead.

const access to matrices

Tags: None
(comma "," separated)
csavorgn
Registered Member
Posts
3
Karma
0
OS

const access to matrices

Mon Dec 19, 2011 11:06 am
Hi,
for a software project I'm working on I need to write a class
which has an Eigen matrix as a member. I wrote functions to access the
Eigen matrix member by reference but I noticed something which I did not
expect. To explain my problem I will use a little example code:

Code: Select all
#include <iostream>
#include "Eigen/Dense"

using namespace std;
using namespace Eigen;

// This is a simplified version of the wrapper class I'm writing
class Wrapper{
  MatrixXd mat;
public:
  Wrapper() {}

  Wrapper(size_t nr, size_t nc) : mat(nr,nc) {}
 
  // function to access Eigen matrix (const version)
  const MatrixXd& eigen() const {
    cout << "## using const version of eigen() ##" << endl;
    return mat;
  }
 
  // function to access Eigen matrix (non-const version)
  MatrixXd& eigen() {
    cout << "## using non-const version of eigen() ##" << endl;
    return mat;
  }

  // function to access const Eigen matrix
  const MatrixXd& eigenConst() const {
    return mat;
  }
};

void printer(const Wrapper& wrap){
  cout << wrap.eigen() << endl;
}

int main(){
  MatrixXd mat1(2, 3);
  Wrapper mat2(2, 3);
  MatrixXd mat3, mat4;

  cout << "Filling mat1" << endl;
  mat1 << 1, 2, 3, 4, 5, 6;
 
  cout << "Filling mat2" << endl;
  mat2.eigen() << 1, 3, 5, 2, 4, 6;
  cout << endl;

  cout << "mat1:" << endl << mat1 << endl << endl;

  cout << "mat2:" << endl;
  cout << mat2.eigen() << endl << endl;

  cout << "printing mat2 using printer" << endl;
  printer(mat2);
  cout << endl;

  cout << "mat3 = mat1 + mat2.eigen()" << endl;
  mat3 = mat1 + mat2.eigen();
  cout << endl;

  cout << "mat3:" << endl << mat3 << endl << endl;

  cout << "mat4 = mat1 + mat2.eigenConst()" << endl;
  mat4 = mat1 + mat2.eigenConst();
  cout << endl;

  cout << "mat4:" << endl << mat4 << endl;
}


After compiling the code and running the executable I get the following:

Filling mat1
Filling mat2
## using non-const version of eigen() ##

mat1:
1 2 3
4 5 6

mat2:
## using non-const version of eigen() ##
1 3 5
2 4 6

printing mat2 using printer
## using const version of eigen() ##
1 3 5
2 4 6

mat3 = mat1 + mat2.eigen()
## using non-const version of eigen() ##

mat3:
2 5 8
6 9 12

mat4 = mat1 + mat2.eigenConst()

mat4:
2 5 8
6 9 12


What I would expect is that when I use the << operator and when I add mat3 = mat1 + mat2.eigen() the const version of eigen() is invoked similarly to when I use the function printer which I defined.

Am I missing something and this is the way it is supposed to work?

Thanks a lot!

Carlo
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: const access to matrices

Mon Dec 19, 2011 12:05 pm
this is just how c++ works, if your object is not const then the non-const versions of the method will be called by default.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]