i have a struct and i need to use an array of that struct. The member of that array is definite at compile time, so i am going to hard code them. how do i declare it in my class header file and fill the items in the class file?

ok i found out how to do that:

test.h :

class test
{
    public:
  static const int myArray[];

};

test.cpp :

#include "test.h"
#include <iostream>
using namespace std;
const int  test::myArray[]={1,2,3,4,5};

int main()
{

for(int i=0;i<5;i++)
{
  cout << test::myArray[i] << endl;
}

}

When i run the code the output is :
1
2
3
4
5

I also suspected if it is mystery of keyword static or const then i also created the same thing without const keyword.

test.h :

class test
{
    public:
  static  int myArray[];

};

test.cpp :

#include "test.h"
#include <iostream>
using namespace std;
 int  test::myArray[]={1,2,3,4,5};

int main()
{

for(int i=0;i<5;i++)
{
  cout << test::myArray[i] << endl;
}

}

the output is the same as the previous :
1
2
3
4
5

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.