简体   繁体   English

64位和32位整数

[英]64-bit and 32-bit integers

Is there a good way to modify a class in C++ so that its integers are 64-bit on a 64-bit system and 32-bit for 32-bit systems? 有没有一种很好的方法来修改C ++中的类,使其整数在64位系统上为64位,在32位系统中为32位? Is there a way to check for that? 有没有办法检查?

The class is something like: 这个类是这样的:

class B{
      public:
            int64_t size();
      private:
            int64_t m_size();
}

If you really want exactly what you said (32-bit on 32-bit and 64-bit on 64-bit) you'll need to use macros. 如果你真的想要你所说的(32位32位和64位64位),你需要使用宏。

But what you probably want instead is to just use size_t . 但你可能想要的只是使用size_t

EDIT: 编辑:

size_t is guaranteed to be large enough to size any object and index any array. size_t保证足够大,可以调整任何对象的大小并索引任何数组。 And as such, it is usually 32-bits on 32-bit and 64-bits on 64-bit. 因此,它通常在32位上为32位,在64位上为64位。 So it probably does exactly what you want. 所以它可能正是你想要的。

intptr_t ,它可以用于你可能想到的邪恶目的。

You could use long int . 你可以使用long int AFAIK long int is the same as int (4 byte integer) in 32-bit compilers and the same as long long int (8 byte integer) in 64-bit compilers. AFAIK long int与32位编译器中的int (4字节整数)相同,与64位编译器中的long long int (8字节整数)相同。 You can check it with sizeof . 你可以用sizeof来检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM