Answer the question
In order to leave comments, you need to log in
How to define conversions from a variable of a simple type to its own type?
The whole question is in the subject.
More specifically, "How to define conversions from a variable of type double to a matrix?"
There is a matrix class.
I understand that this can be done simply by throwing a double number into the constructor and generating a 1x1 matrix.
Is it possible to implement this by redefining operators?
// Пример того, как это может выглядеть:
int tmpNum = 99;
Matrix<MatrixType> tmpMatr = static_cast< Matrix<MatrixType> >(tmpNum);
Answer the question
In order to leave comments, you need to log in
Nobody forbids you to redefine the assignment operator and mathematical operators.
In general, google it, but if memory serves, you can redefine = + - / * % << >>
Links for understanding?
habrahabr.ru/post/132014
en.cppreference.com/w/cpp/language/operators
https://ideone.com/YVpHpL
protected:
double number;
public:
Matrix(double n):number(n){
///....
}
...
Matrix m = 10.2; //тут уже сработает конструктор и никаких приведений типов не нужно
If I only have int data[MATRIX_MAX_SIZE][MATRIX_MAX_SIZE] then how do I write a constructor?Initialization lists will save you.
int data[2][2] = { {1, 2},
{3, 4} };
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question