Variable Variable Methods

I can guess this is a newbie question, but I seem to have missed it somewhere, plus I'm a newbie, so if the answer is obvious, not too mean ...

I saw variables declared in several ways:

DataType varName = value;
DataType varName(value);
DataType varName{value};

I thought this was one of those situations when scriptwriters were selected, but lately I tried to add 2 char variables and set it to another char variable, and I used the bracket version:

unsigned char sum{ char1 + char2 };

And I get the error message: Error 1 error C2398: Element '1': converting from 'int' to 'unsigned char &' requires narrowing the conversion.

But I do not get this error:

unsigned char sum(char1 + char2);

So can someone explain the difference between the ads or provide a link to the site that will be?

Thanks...

+4
2

TL; DR: char s, int, .

++, C, ; - char1 + char2 (, char1 char2 char) char, , int. §4.5 ( ), §4.13 ( ) §5.7 ( , ).

, unsigned char sum {char1 + char2};, , (§8.5.3), . int char ( - *), , .

, , : " , ?"


* ++ , int , INT_MIN INT_MAX (§3.9.2) C (§18.3.3.2), ( E, § 1) -32767 +32767 **. , int . char, , . C char (§6.2.5.3, . 5.2.1), (. 3.6) " , , ". , , char , , .

** -32767, -32768. C .

+1

, value DataType ctor explicit. ctor value, , explicit. , , .

0

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


All Articles