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? 1

Status
Not open for further replies.

ascht

Programmer
Sep 22, 2000
43
CH
As palbano told me I coul manage that every instance of this class has automatically this constant inside.
Bu I also want to have a constant array, readonly. For example like that:

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

palbano wrote me to put the following line in a .cpp file

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

But I didnt find a way to make it work. Are there details I should look at? Could someone help me?
 
In part what is wrong is Protocol[2] should be Protocol[1] if your index starts at '0'. Granted this shouldn't cause it to fail but if you are checking Protocol[1] and Protocol[2] it will fail.



 
ascht,

It works fin in my code (VC++ version 6 on NT 4 Workstation). With the code I posted I can then do this:
TRACE("Protocol[0]: %s\r\n", cTest::protocol[0]);
Which then displays this in the debuger output window:
Protocol[0]: TCPIP

> But I didnt find a way to make it work.

Please explain.

-pete
 
Now, it's well working. I found out that the line
LPCTSTR const cTest::protocol[2] ={"TCPIP","MATIP"};
can be positioned on places, where

- the namespace is global, not within a function
- the class is known (somewhere after the class declaration)
- independent the place where the class is instanced

Tanks
Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top