I'm writing a linear algebra library (long story short, it's a school assignment) that involves matrices, vectors, etc. In the process of creating this library, I'm going to be creating functions that perform mathematical operations on objects. For example, transpose matrix, invert matrix, normalize vector, etc.
I was curious as to what is the "best practice" for this sort of function… That is, should I make the function a member function, or non-member? (For clarity/library use sake)
Example:
//Member function way:
B = A.transpose();
C = A.inverse();
//Non-member function way:
B = linalg::transpose(A); //Non-member transpose function in linear algebra namespace
C = linalg::inverse(A);
Is there some standard regarding these sorts of operations? Or, at least, is there a common way people do this? I'm leaning towards the first option, but I'd like to know if this is recommended.
Best Answer
This is just a matter of style and taste. I have seen different linear algebra libraries, either
written in an OOP style using member functions
written in a non-OOP style using only free functions
providing an API with both
and all of them were working. At least, your API should be consistent, so pick your choice and make sure you don't mix those styles arbitrarily.