Tuesday, 15 May 2012

Type casting struct in C++ -



Type casting struct in C++ -

in c++ facing difficulties! want know c++ equivalent of these c statement?

struct stat sb; // struct, same in c++ printf("i-node number: %ld\n", (long) sb.st_ino); // c statement // c++ statement of above statement cout<<" inode number: "<< (long) sb.st_ino; // right way? // or 1 cout<<" inode number: "<< (long) (long) sb.st_ino; // right way?

although c-style casts work in c++ considered improve style utilize c++ style casts. utilize static_cast in cases list. so:

cout<<" inode number: "<< static_cast<long>(sb.st_ino);

also in lastly illustration there no sense in casting look twice same type. if seek write cast long long use:

cout<<" inode number: "<< static_cast<long long>(sb.st_ino);

also dynamic_cast, const_cast, reinterpret_cast(last 2 should avoided whenever possible).

c++

No comments:

Post a Comment