32

Pretty self-explanatory. The array is of an integral type, the contents are known and unchanging, and C++0x isn't allowed. It also needs to be declared as a pointer. I just can't seem to find a syntax that works.

The declaration in Class.hpp:

static const unsigned char* Msg;

Stuff in Class.cpp is really what I've tinkered with:

const unsigned char Class::Msg[2] = {0x00, 0x01}; // (type mismatch)
const unsigned char* Class::Msg = new unsigned char[]{0x00, 0x01}; // (no C++0x)

...etc. I've also tried initializing inside the constructor, which of course doesn't work because it's a constant. Is what I'm asking for impossible?

11
  • Is Msg declared static in the header and the implementation file?
    – user195488
    Commented Jul 6, 2012 at 17:44
  • 6
    Arrays decay into pointers, though, so isn't an array declaration fine?
    – chris
    Commented Jul 6, 2012 at 17:44
  • 2
    Well no wonder, there is a type mismatch. You are confusing arrays and pointers.
    – user195488
    Commented Jul 6, 2012 at 17:45
  • 1
    @ACK_stoverflow, Any array (non-reference) you pass into a function will be decayed into a pointer, so declaring an array and passing it into a function expecting a pointer works fine.
    – chris
    Commented Jul 6, 2012 at 17:48
  • 1
    @ACK_stoverflow, This compiles fine with -pedantic, and passes an array into a function taking a pointer.
    – chris
    Commented Jul 6, 2012 at 17:53

2 Answers 2

51
// in foo.h
class Foo {
    static const unsigned char* Msg;
};

// in foo.cpp
static const unsigned char Foo_Msg_data[] = {0x00,0x01};
const unsigned char* Foo::Msg = Foo_Msg_data;
7
  • 4
    +1, for a solution that uses a pointer and yet avoids the hassle of memory management. Commented Jul 6, 2012 at 17:48
  • We have a winner! I was hoping there was a more compact way, but this definitely does what I was asking for. Nicely done, posted 5 minutes after the question was asked no less! Commented Jul 6, 2012 at 17:52
  • Why does the declaration in the header have to be static, please?
    – RAAC
    Commented Apr 6, 2014 at 14:47
  • @RAAC because you need it to be static, so that it can be accessed without instantiating an object from the class
    – Anubis
    Commented Sep 25, 2014 at 4:49
  • 1
    shouldn't the definition not have a static qualifier? gcc 4.9.2 tells me to remove it from foo.cpp. Commented Oct 5, 2015 at 3:56
33

You are mixing pointers and arrays. If what you want is an array, then use an array:

struct test {
   static int data[10];        // array, not pointer!
};
int test::data[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

If on the other hand you want a pointer, the simplest solution is to write a helper function in the translation unit that defines the member:

struct test {
   static int *data;
};
// cpp
static int* generate_data() {            // static here is "internal linkage"
   int * p = new int[10];
   for ( int i = 0; i < 10; ++i ) p[i] = 10*i;
   return p;
}
int *test::data = generate_data();
3
  • 2
    +1 the confusion probably lies because you can access members of a char* in an "array-like" fashion.
    – user195488
    Commented Jul 6, 2012 at 17:46
  • Sorry, I should have been more specific: what I need is a const pointer to an array. Commented Jul 6, 2012 at 17:54
  • Why dynamically allocate the memory if the size is known at compile time? The array can be static, and still initialized with a loop at run-time (instead of having a bit table as part of your executable). That allows the compiler to optimize by removing the extra level of indirection, when it can see the pointer assignment (like the other answer suggests). Commented Oct 2, 2016 at 2:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.