-3

after 30 min of googling I decided to ask here my doubt It maybe invalid question if I am not understanding below line of code correctly

result += char(int(text[i]+s-65)%26 +65);

In above code char() is a function ? If yes so I am unable to find any information about it and if no so what is this ? And same doubt for inner int() .

Above code is copied from a C++ program.

Aux
  • 85
  • 2
  • 3
  • 10
  • 2
    They are casts. I think you need to read a good C++ textbook. –  Jun 20 '18 at 16:50
  • Can you give a reference link @Neil Butterworth – Aux Jun 20 '18 at 16:52
  • In C++, this is known as a [functional cast](https://en.cppreference.com/w/cpp/language/explicit_cast): "*The functional cast expression consists of a simple type specifier or a typedef specifier (in other words, a single-word type name: `unsigned int(expression)` or `int*(expression)` are not valid), followed by a single expression in parentheses. **This cast expression is exactly equivalent to the corresponding C-style cast expression**.*" – Remy Lebeau Jun 20 '18 at 16:52
  • http://en.cppreference.com/w/cpp/language/explicit_cast –  Jun 20 '18 at 16:55
  • Whoever wrote that line of code needs a different job. – R Sahu Jun 20 '18 at 16:59

1 Answers1

2

They're not functions. They're just alternate syntax for type-casting. char(x) is more-or-less equivalent to static_cast<char>(x).

In general, in C++, one should prefer the C++-specific constructs for casting objects (static_cast, dynamic_cast, const_cast, and reinterpret_cast), as those help ensure you don't do anything dumb when casting objects. So in your code example, I'd recommend rewriting it as

result += static_cast<char>(static_cast<int>(text[i]+s-65)%26 +65);

But functionally, it's all identical.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Xirema
  • 19,889
  • 4
  • 32
  • 68
  • @Remy Lebeau `char(x)` is equivalent to `(char) x`? – Aux Jun 20 '18 at 17:04
  • 2
    @Aux Yes. I'm not sure what the order of operations is for stuff like `char(x) + 200` vs `(char)x + 200`, but `T(...)` and `(T)...` is equivalent for any type which doesn't have a constructor that takes `...` as arguments. – Xirema Jun 20 '18 at 17:07
  • 2
    @Xirema See [C++ Operator Precedence](http://en.cppreference.com/w/cpp/language/operator_precedence). `char(x)` and `(char)x` both have a higher precedence than `+`. – Remy Lebeau Jun 20 '18 at 17:25
  • "But functionally, it's all identical" - Not quite. A C-style case (and a function-style cast) will start out trying to do a `static_cast` but if that fails it will attempt other casts, eventually degenerating into a `reinterpret_cast` followed by a `const_cast` (rarely what you want). The C++ casts don't have that problem (of trying multiple types of casts and just going with whatever succeeds first) - that's why C++ casts are safer and you should never write C-style (or function-style) casts in your code; it's really hard to work out what conversion they will *actually* end up doing. – Jesper Juhl Jun 20 '18 at 18:30