c++中的类型转换结构
Type casting struct in C++
在c++中我面临着一些困难!我想知道这些c语句在c++中的等价是什么?
struct stat sb; // this is struct, will be same in c++
printf("I-NODE NUMBER: %ldn", (long) sb.st_ino); // this is C statement
// c++ statement of above statement
cout<<" Inode number: "<< (long) sb.st_ino; // is this the correct way?
// or this one
cout<<" Inode number: "<< (long) (long) sb.st_ino; // is this the correct way?
虽然c风格的强制转换可以在c++
中工作,但使用C++
风格的强制转换被认为是更好的风格。那就是在你列出的情况下使用static_cast
。像这样:
cout<<" Inode number: "<< static_cast<long>(sb.st_ino);
同样,在最后一个例子中,将表达式两次强制转换为同一类型是没有意义的。如果您要写的是转换为long long
,请使用:
cout<<" Inode number: "<< static_cast<long long>(sb.st_ino);
还要查找dynamic_cast, const_cast, reinterpret_cast(最后两个应该尽可能避免使用)。
在c++中,您可能只需要执行以下操作。完全不需要强制转换。
std::cout << " Inode number: " << sb.st_ino;
相关文章:
- 从基本类型转换为非 POD 结构
- 类型转换自定义结构
- 在 c++ 中将结构类型转换为无符号字符*
- 将指向结构的指针转换为具有较少字段数的另一种结构类型
- C 中STL结构的类型转换
- 强制转换为使用typedef创建的结构类型
- 继承结构的类型转换导致g++编译错误
- 如何将迭代器类型转换为其原始结构
- 使用结构进行隐式类型转换
- 构造函数重载选择了强制转换运算符而不是结构类型
- 易失性结构与类型转换
- 将字符数组类型转换为结构体(不是类/对象)
- 如何将LRESULT强制转换为自定义结构类型
- 从字节[]到结构的类型转换
- 将argv类型转换为结构类型
- 将字符缓冲区类型转换为来自套接字的结构
- c++中的类型转换结构
- Boost Fusion:将适应的结构体类型转换为文本
- std::unique_ptr结构成员转换为结构类型
- 使用指针成员对结构体进行类型转换