F
F
Flaker2015-09-08 13:45:59
C++ / C#
Flaker, 2015-09-08 13:45:59

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

3 answer(s)
A
Antony, 2015-09-08
@Flaker

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

A
Alexander Taratin, 2015-09-08
@Taraflex

https://ideone.com/YVpHpL

protected:
  double number;
public:
  Matrix(double n):number(n){
    ///....
  }
...
Matrix m = 10.2; //тут уже сработает конструктор и никаких приведений типов не нужно

M
Maxim Moseychuk, 2015-09-08
@fshp

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 question

Ask a Question

731 491 924 answers to any question