Trisha
Trisha

Reputation: 141

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? Is there a way to check for that?

The class is something like:

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

Upvotes: 3

Views: 736

Answers (3)

saeedn
saeedn

Reputation: 3378

You could use 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. You can check it with sizeof.

Upvotes: 1

Mysticial
Mysticial

Reputation: 471209

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.

But what you probably want instead is to just use size_t.

EDIT:

size_t is guaranteed to be large enough to size any object and index any array. And as such, it is usually 32-bits on 32-bit and 64-bits on 64-bit. So it probably does exactly what you want.

Upvotes: 6

Simon Richter
Simon Richter

Reputation: 29586

There is intptr_t, which can be used for the nefarious purposes you probably have in mind.

Upvotes: 2

Related Questions