The purpose of the double structure

I came across something the other day in a colleague's code, and I want to understand how / why it works.

It uses a structure like this

 struct my_struct
 {

   my_struct(){ /* default constructor*/};
   my_struct(char c){ /*some special constructor*/};
   // other stuff
   my_struct& operator=(const my_struct &ms){ /* assignment */};
  };

Initially, through a typo, he found out that the following works

  my_struct ms;
  double a;
  ms = a;

I found out that this is equal (in the sense of giving the same final ms structure) to the following

  my_struct ms;
  double a;
  my_struct ms2((char) a);
  ms=ms2;

But I do not know why. Personally, I think this should not work, because there is no assignment operator for double and my_struct, and also because for my_struct there is no constructor using double.

I tried to do this, but did not find anything suitable.

@ user657267: I wanted to keep it as short as possible, you could add the line a = 5; or whatever is applicable.

+4
source share
2 answers

:

ms = a;

my_struct& operator ()(double)
my_struct& operator ()(double&)
my_struct& operator ()(const double&)

double. . :

my_struct& operator=(const my_struct &ms)

, const- my_struct. , ( double:

my_struct(double)
my_struct(double&)
my_struct(const double&)

, . , .

, , . , double , , . , -

my_struct(<<something that can be converted from a double>>)

explicit. , :

my_struct(char)

double char ( , , , ). (, ) , , .

, , explicit, , . explicit . :

explicit my_struct(char)

double char. ; :

char x = 'a';
my_struct ms;
ms = x;

my_struct char, . , ( double) ( double). , .

+4

:

my_struct(char c)

, , char, my_struct.

, explicit :

explicit my_struct(char c)

double :

double a = my_initial_value;
my_struct ms2(a);

, , int, float, .. .

+3

Source: https://habr.com/ru/post/1547552/


All Articles