C++: char(nextchar);它有什么作用
C++: char(nextChar); what doess it do
这有什么作用:char(nextChar)
.我没有名为 char 的变量。 我在这里调用 char 类构造函数还是什么?
int nextChar;
while ((nextChar == stream.get()) != EOF)
{
// Convert it to a string for lookup in the symbol table
string foundChar = "";
foundChar += char(nextChar);
}
它使用重载std::string::operator += (char)
将char(nextChar)
追加到std::string
foundChar
,然后丢弃字符串。
char(nextChar)
是从int
到char
的强制转换(因为nextChar
被声明为int
)——相当于(char)nextChar
。
语法
T(exp)
是一个强制转换,等效于(T)(exp)
(但T()
对应于默认构造函数,T(exp1, exp2, ...)
调用相应的构造函数)。这意味着
int* ptr;
int i = int(ptr);
是允许的(在与reinterpret_cast<int>(ptr)
相同的条件下和相同的含义),而
int j = static_cast<int>(ptr);
int k(ptr);
不。
此操作的行为未定义,因为
while ((nextChar == stream.get()) != EOF)
不会断言stream.get()
nextChar
而是比较两个值。在那之后,nextChar仍然保存着它在(丢失的)初始化后所做的内存垃圾。
可能它的目的是分配值并将其与 EOF 进行比较:
while ((nextChar = stream.get()) != EOF)
此外,char(nextChar)
有效地与更常用的(char)nextChar
相同,甚至更好的static_cast<char>(nextChar)
顺便说一句:
int nextChar;
while ((nextChar = stream.get()) != EOF) { }
可以安全地缩短为
while ((int nextChar = stream.get()) != EOF) { }
只要你不需要nextChar
圈外。
char(nextChar) 是从 int 数据类型转换为 char 数据类型的类型转换 - 相当于 (char)nextChar
有关更多详细信息,请转到下面的链接
相关文章:
- archive_read_support_filter_all() 有什么作用?
- 当我们为(;;) 写作时,它做了什么?for 循环中的双分号有什么作用?
- 这个递归函数有什么作用?运行时的复杂性是多少?
- DrawShadow 中的 Elevation 究竟有什么作用?
- 测试"stream >> char"有什么作用?
- "uFlags &= ~CHN_PANNING"命令有什么作用?
- GCC 的 -Wpsabi 选项究竟有什么作用?压制它意味着什么?
- int** a = new int*[n]();这个函数有什么作用?
- C++ 此函数调用之前的(void)有什么作用?
- i+=(i&-i) 有什么作用?它是便携式的吗?
- 这个size_t在 lambda 中有什么作用?C++代码
- 数组下标运算符 ([ ]) 对数组有什么作用?
- 结构之外的节点 * head 有什么作用?
- 重载括号运算符有什么作用?
- clang的"-Ofast"选项在实践中有什么作用,特别是对于与gcc的任何差异?
- std::make_unique_for_overwrite() 对 std::make_unique() 有什么作用?
- 运算符 delete[] 有什么作用?
- 下面的代码有什么作用?c++
- 这个陈述'p = I.ptr<uchar>(i);有什么作用?
- 这个嵌套的名称空间定义有什么作用