2011年4月25日 星期一

Static Const Array in C++ Class

class A
{
    static const int dd[3] = { 1, 2, 3 };
};

error: a brace-enclosed initializer is not allowed here before '{' token
error: invalid in-class initialization of static data member of non-integral type 'const int [3]'

網路上找到的解法是:
class A
{
    static const int dd[3];
};
const int A::dd[3] = { 1, 2, 3 };

===========================================
Static Const Initialised Structure Array in C++ Class
http://stackoverflow.com/questions/4605058/static-const-initialised-structure-array-in-c-class
===========================================
class c
{
public:
  struct p
  {
    int a;
    int b;
  };
  static const p pp[2];
};

const c::p pp[2] =  { {1,1},{2,2} };

int main(void)
{
  class c;
  return 0;
}

===========================================
class c
{
private:
  struct p
  {
    int a;
    int b;
  };
  static const p pp[2];
};

const c::p pp[2] =  { {1,1},{2,2} };

int main(void)
{
  class c;
  return 0;
}
const c::p c::pp[2] =  { {1,1},{2,2} };

沒有留言:

張貼留言