Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare array size in header file without #define's

Tags:

c++

arrays

header

I have a code a following (simplified version):

#define MESSAGE_SIZE_MAX 1024
#defined MESSAGE_COUNT_MAX 20

class MyClass {
public:
   .. some stuff
private:
   unsigned char m_messageStorage[MESSAGE_COUNT_MAX*MESSAGE_SIZE_MAX];
};

I don't like defines, which are visible to all users of MyCalss.

How can I do it in C++ style?

Thanks Dima

like image 860
dimba Avatar asked Feb 13 '26 16:02

dimba


1 Answers

Why don't you simply use a constant?

const int message_size_max = 1024;

Note that unlike C, C++ makes constant variables in global scope have static linkage by default.

The constant variable above is a constant expression and as such can be used to specify array sizes.

char message[message_size_max];
like image 69
avakar Avatar answered Feb 15 '26 06:02

avakar