1

My OOP-style C++ application has a method (used in many .cpp files), that get two parameters: pointer to array and array size:

bool SendData(unsigned char* data, size_t size);

I have some arrays, that will never changes. And I want to define them, or make a consts, or something else, for doing this:

bool result1, result2;
unsigned char arr2[] = {0x2, 0x5, 0x6};
result1 = SendData(PREDEFINED_ARRAY_1, sizeof(PREDEFINED_ARRAY_1));
result2 = SendData(arr2, sizeof(arr2));

I try to solve it, using constants - just write in header file:

static const unsigned char PREDEFINED_ARRAY_1[] = {0x1, 0x3, 0x5};
static const unsigned char PREDEFINED_ARRAY_1[] = {0x2, 0x3, 0x4, 0x5, 0x6, 0x7};

But get compiler errors about '{' and '}'. It's confuse me, becouse I can write:

static const unsigned char c = 10;

And all will be ok. I can initialize variables, but can't init arrays? It's strange.

Yes, I know, that static variables should be initialized in cpp-files, but I have many files, that use this function with constants, so, as I know, It's a dead way. I show this code only to explain what I want.

Also I don't want to write something like this in constructor (or special function) after definition in class:

PREDEFINED_ARRAY_1[0] = 0x1;
PREDEFINED_ARRAY_1[1] = 0x3;
PREDEFINED_ARRAY_1[2] = 0x5;

Because the number of arrays is about 100 andlength ~20-30 symbols per each. Too many code for simple thing. Or not?

Then, I try to create special class:

class Array
{
public:
    void Set(unsigned char symbol, ...); // using va_args
    unsigned char* GetData();
    size_t GetSize();

private:
    unsigned char* data;
    size_t size;
};

class MainClass
{
public:
    MainClass();
    void Send();

    Array PREDEFINED_ARRAY_1;
    Array PREDEFINED_ARRAY_2;
};

MainClass::MainClass()
{
    PREDEFINED_ARRAY_1.Set(3, 0x1, 0x3, 0x5);
    PREDEFINED_ARRAY_1.Set(6, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7);
}

void MainClass::Send()
{
    SendData(PREDEFINED_ARRAY_1.GetData(), PREDEFINED_ARRAY_1.GetSize());
}

But in this case, I should manually count number of symbols. Maximum length may be about 50 symbols and the risk of mistake is to high. I want to avoid it.

Third and dumbest way is something like this:

#define PREDEFINED_ARRAY_1 {0x1,0x3,0x5}

int main()
{
    unsigned char arr1[] = PREDEFINED_ARRAY_1;
    SendData(arr1, sizeof(arr1));
}

But it is too ugly.

My question is: What the best practice for doing it?

P.S. As I already say, I'm using OOP paradigm and want to avoid global variables and other C-things.

4
  • 1
    What compiler errors? Commented Dec 20, 2013 at 15:07
  • @JohnDibling, C2059 (syntax error: '{') and C2334 (unexpected token(s) preceding '{'; skipping apparent function body). Commented Dec 20, 2013 at 15:12
  • You should make the SendData accept const data or make the array non-const... Otherwise you have constness problems. Commented Dec 20, 2013 at 15:20
  • @leemes I'm talking about this case: ideone.com/Kxs7To Commented Dec 20, 2013 at 15:28

1 Answer 1

1

You can declare the arrays in the header file, but give them the full type information for C-style arrays (i.e. a length). Then define the array contents in the implementation file.

Then, the array contents "live" in the translation unit of the corresponding .cpp file (once in your project) but every inclusion of the header knows enough to even use sizeof correctly.

// .h
class MainClass {
public:
    static const unsigned char predefinedArray1[3];
    static const unsigned char predefinedArray2[5];
};

// .cpp
const unsigned char MainClass::predefinedArray1[] = { 'x', 'y', 'z' };
const unsigned char MainClass::predefinedArray2[] = { 'a', 'b', 'c', 'd', 'e' };

Demo with usage: http://ideone.com/UK0EOv

Sign up to request clarification or add additional context in comments.

11 Comments

Thank you. But that mean, that I manually should count number of symbols in array? And there are no way to avoid it? Because if I mistake and write array[4], but array will have only 3 symbols, then it compiles nice, but in run-time I'll get a problems.
Hm, no it should not compile: ideone.com/EpMrON (declared with 5, assigned 6)
Oh, but if you assign less than declared, it compiles indeed... Let me think
Same problem with the (still better) std::array which is available if you have C++11. Still thinking how you can avoid this...
Yeah the manual counting is the problem we should really solve. Please try this one, please try with multiple translation units. That's where it gets interesting.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.