As an example, I have a class that uses the Eigen library as follows:
class A
{
private:
Eigen::MatrixXd _matrix;
public:
Eigen::MatrixXd GetMatrix() const;
}
GetMatrix() as of right now is implemented as such:
Eigen::MatrixXd A::GetMatrix() const
{
return _matrix;
}
Let's say in main.cpp, I have a function that does the following:
void PrintMatrix(const Eigen::MatrixXd &matrix)
{
std::cout<<"Print matrix: "<<matrix<<std::endl;
}
int main()
{
A obj;
PrintMatrix(obj.GetMatrix());
return 0;
}
In the GetMatrix() function, my goal is to return the _matrix value, and it will be used as seen in main(). I would like the most efficient approach.
I am new to optimization and just starting to warm up to pointers. As you guys can see, GetMatrix() currently returns a value, and I am aware that returning by reference and returning by pointer is much faster than by value, but what are the risks/pros and cons of doing either? And which of the two would be preferable for my case?