Using extern

R

ruffiano

I have a file called first.cpp that contains the following
declarations:
static const char *MY_STRING1 = "My first string";
static const char *MY_STRING2 = "My second string"

If I wanted to use MY_STRING1 and MY_STRING2 in a header file, would
the following be correct?
extern static const char *MY_STRING1;
extern static const char *MY_STRING2;

Thanks in advance for your input.
 
I

Ian Collins

ruffiano said:
I have a file called first.cpp that contains the following
declarations:
static const char *MY_STRING1 = "My first string";
static const char *MY_STRING2 = "My second string"

If I wanted to use MY_STRING1 and MY_STRING2 in a header file, would
the following be correct?
extern static const char *MY_STRING1;
extern static const char *MY_STRING2;

Thanks in advance for your input.
No, you have to drop the static.
 
D

David Harmon

On Tue, 21 Nov 2006 18:28:18 -0800 in comp.lang.c++, "Chris Thomasson"
I would also make the pointers themselves constant...

What's the point (sic) of having pointers if they are constant?
If you cannot reassign them, then just make the char arrays extern in
the first place. No?
 
C

Chris Thomasson

Ian Collins said:
No, you have to drop the static.

I would also make the pointers themselves constant...

static const char* const MY_STRING1 = "My first string";
static const char* const MY_STRING2 = "My second string";


extern const char* const MY_STRING1;
extern const char* const MY_STRING2;


You can also do this:


static const char* prvMY_STRING1 = "My first string";
static const char* prvMY_STRING2 = "My second string";
const char* const MY_STRING1 = prvMY_STRING1;
const char* const MY_STRING2 = prvMY_STRING2;


extern const char* const MY_STRING1;
extern const char* const MY_STRING2;


for more flexibility...
 
I

Ian Collins

David said:
On Tue, 21 Nov 2006 18:28:18 -0800 in comp.lang.c++, "Chris Thomasson"



What's the point (sic) of having pointers if they are constant?
If you cannot reassign them, then just make the char arrays extern in
the first place. No?
Isn't that what he was suggesting? I agree, the pointers should be
constant.
 
D

David Harmon

On Wed, 22 Nov 2006 15:29:36 +1300 in comp.lang.c++, Ian Collins
Isn't that what he was suggesting? I agree, the pointers should be
constant.

No. Extern arrays, no pointers -

..cpp:
extern const char MY_STRING1[] = "My first string";
extern const char MY_STRING2[] = "My second string";

..h:
extern const char MY_STRING1[];
extern const char MY_STRING2[];
 
I

Ian Collins

David said:
On Wed, 22 Nov 2006 15:29:36 +1300 in comp.lang.c++, Ian Collins
Isn't that what he was suggesting? I agree, the pointers should be
constant.


No. Extern arrays, no pointers -

..cpp:
extern const char MY_STRING1[] = "My first string";
extern const char MY_STRING2[] = "My second string";

..h:
extern const char MY_STRING1[];
extern const char MY_STRING2[];
Ah, I understand you now!
 
C

Chris Thomasson

David Harmon said:
On Tue, 21 Nov 2006 18:28:18 -0800 in comp.lang.c++, "Chris Thomasson"


What's the point (sic) of having pointers if they are constant?
If you cannot reassign them, then just make the char arrays extern in
the first place. No?

Thats fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top