Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I define constant string arrays as class members?

Status
Not open for further replies.

ascht

Programmer
Sep 22, 2000
43
CH
class cTest
{
public:
const int NUM_PROTOCOLS=2;
const string Protocol[NUM_PROTOCOLS]={"TCPIP"
"MATIP"};
};

I'd like that every instance of this class has automatically this array.
It should be readonly. I don't know how to initalize it.
Do you have any idea? Or an alternative solution, without #define?
 
ascht,

First you must initialize const members using the initialization list. However you cannot initialize array members. So now what? Well to quote your post:

> I'd like that every instance of this class has automatically this array.
> It should be readonly

That sounds like a static member to me and as it happens you can initialize a static array. Now here is what the class would look like:


class cTest
{
public:
cTest():NUM_PROTOCOLS(2){}
const int NUM_PROTOCOLS;
static const LPCTSTR Protocol[2];
};


Then in the .cpp file you initialize the array like this:

LPCTSTR const cTest::protocol[2] = {"TCPIP","MATIP"};


Hope this helps
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top