By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

Replace #define with const???

P: n/a
Hi, All

When I look at effective c++,item2 and item3.
I have some basic questions , Does anyone be familar with this topic?

it suggests const is perfer to #define, then I think how to
replace #define with const.

example:
2 header file StringGrid1.h StringGrid2.h
correspond to 2 implement file StringGrid1.cpp and StringGrid2.cpp

StringGrid1.cpp include StringGrid1.h
StringGrid2.cpp include StringGrid2.h and StringGrid1.h

In StringGrid1.h has the code:
#define Grid1_Col_Num 10 // represent StringGrid1 column number
#define Grid1_Rol_Num 10 // row number
StringGrid2.h has the analogue code.
I try to replace "#define Grid1_Col_Num 10 " with something in
StringGrid1.h
belows are condition and running result
1. Replace "#define Grid1_Col_Num 10 " with "int Grid1_Col_Num = 10;"
Grid1_Col_Num in two cpp have different address. And have link
warning "__Grid1_Col_Num" defined in both module.
That's ok. I know the reason.

2. Replace "#define Grid1_Col_Num 10 " with "const int Grid1_Col_Num =
10;"
Grid1_Col_Num in two cpp file have the same address. Why??
I guess it should have different address and values are const, but
it does not.

3. Replace "const int Grid1_Col_Num = 10" with "static int
Grid1_Col_Num = 10;"
Grid1_Col_Num in two cpp file have the same address

4. Replace "const int Grid1_Col_Num = 10" with "static const int
Grid1_Col_Num = 10;"
Grid1_Col_Num in two cpp file have the same address
In 3 4, I think static meaning is that Grid1_Col_Num in all
application have only one copy.
Is it right?
5. so how to use const to replace define in header file????

Aug 9 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
MoslyChang wrote:
Hi, All

When I look at effective c++,item2 and item3.
I have some basic questions , Does anyone be familar with this topic?

it suggests const is perfer to #define, then I think how to
replace #define with const.

example:
2 header file StringGrid1.h StringGrid2.h
correspond to 2 implement file StringGrid1.cpp and StringGrid2.cpp

StringGrid1.cpp include StringGrid1.h
StringGrid2.cpp include StringGrid2.h and StringGrid1.h

In StringGrid1.h has the code:
#define Grid1_Col_Num 10 // represent StringGrid1 column number
#define Grid1_Rol_Num 10 // row number
StringGrid2.h has the analogue code.
I try to replace "#define Grid1_Col_Num 10 " with something in
StringGrid1.h
belows are condition and running result
1. Replace "#define Grid1_Col_Num 10 " with "int Grid1_Col_Num = 10;"
Grid1_Col_Num in two cpp have different address. And have link
warning "__Grid1_Col_Num" defined in both module.
That's ok. I know the reason.

2. Replace "#define Grid1_Col_Num 10 " with "const int Grid1_Col_Num =
10;"
Grid1_Col_Num in two cpp file have the same address. Why??
I guess it should have different address and values are const, but
it does not.

3. Replace "const int Grid1_Col_Num = 10" with "static int
Grid1_Col_Num = 10;"
Grid1_Col_Num in two cpp file have the same address

4. Replace "const int Grid1_Col_Num = 10" with "static const int
Grid1_Col_Num = 10;"
Grid1_Col_Num in two cpp file have the same address
In 3 4, I think static meaning is that Grid1_Col_Num in all
application have only one copy.
Is it right?
5. so how to use const to replace define in header file????
see TPLC++ 5.4 Constants [ptr.const]
Aug 9 '07 #2

P: n/a
On Aug 9, 12:02 pm, MoslyChang <MoslyCh...@gmail.comwrote:
When I look at effective c++,item2 and item3.
I have some basic questions , Does anyone be familar with this topic?
it suggests const is perfer to #define, then I think how to
replace #define with const.
example:
2 header file StringGrid1.h StringGrid2.h
correspond to 2 implement file StringGrid1.cpp and StringGrid2.cpp
StringGrid1.cpp include StringGrid1.h
StringGrid2.cpp include StringGrid2.h and StringGrid1.h
In StringGrid1.h has the code:
#define Grid1_Col_Num 10 // represent StringGrid1 column number
#define Grid1_Rol_Num 10 // row number
StringGrid2.h has the analogue code.
What "analogue" code?
I try to replace "#define Grid1_Col_Num 10 " with something in
StringGrid1.h
belows are condition and running result
1. Replace "#define Grid1_Col_Num 10 " with "int Grid1_Col_Num = 10;"
Grid1_Col_Num in two cpp have different address.
That's wrong. You have undefined behavior, so I suppose
anything the compiler does is legal, but normally, the variable
Grid1_Col_Num has external linkage, which means that all use of
the symbol refers to the same object.

Note that without the const, Grid1_Col_Num is not a constant
expression, and cannot be used in contexts requiring a constant
expression.
And have link
warning "__Grid1_Col_Num" defined in both module.
That's ok. I know the reason.
2. Replace "#define Grid1_Col_Num 10 " with "const int Grid1_Col_Num =
10;"
Grid1_Col_Num in two cpp file have the same address. Why??
That's wrong, and a serious error if it occurs. Because of the
const, the two variables have internal linkage, and the symbol
in one translation unit does not refer to the same variable in
another translation unit.
I guess it should have different address and values are const, but
it does not.
Either it does, or your compiler is seriously broken.
3. Replace "const int Grid1_Col_Num = 10" with "static int
Grid1_Col_Num = 10;"
Grid1_Col_Num in two cpp file have the same address
Again, external linkage, and you should have an instance in each
translation unit. If not, time to change compilers.
4. Replace "const int Grid1_Col_Num = 10" with "static const int
Grid1_Col_Num = 10;"
The static is implicit when you declare const (although IMHO, it
is good practice to use). Linkage is internal, and each
translation unit gets its own instance.
Grid1_Col_Num in two cpp file have the same address
In 3 4, I think static meaning is that Grid1_Col_Num in all
application have only one copy.
Is it right?
5. so how to use const to replace define in header file????
The usual solution would be simply:

int const Grid1_Col_Num = 10 ;
int const Grid1_Rol_Num = 10 ;

in the header file.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 9 '07 #3

P: n/a
On 8 9 , 11 24 , James Kanze <james.ka...@gmail.comwrote:
On Aug 9, 12:02 pm, MoslyChang <MoslyCh...@gmail.comwrote:
When I look at effective c++,item2 and item3.
I have some basic questions , Does anyone be familar with this topic?
it suggests const is perfer to #define, then I think how to
replace #define with const.
example:
2 header file StringGrid1.h StringGrid2.h
correspond to 2 implement file StringGrid1.cpp and StringGrid2.cpp
StringGrid1.cpp include StringGrid1.h
StringGrid2.cpp include StringGrid2.h and StringGrid1.h
In StringGrid1.h has the code:
#define Grid1_Col_Num 10 // represent StringGrid1 column number
#define Grid1_Rol_Num 10 // row number
StringGrid2.h has the analogue code.

What "analogue" code?
In StringGrid2.h
#define Grid2_Col_Num 10
#define Grid2_Col_Num 10
.....
>
I try to replace "#define Grid1_Col_Num 10 " with something in
StringGrid1.h
belows are condition and running result
1. Replace "#define Grid1_Col_Num 10 " with "int Grid1_Col_Num = 10;"
Grid1_Col_Num in two cpp have different address.

That's wrong. You have undefined behavior, so I suppose
anything the compiler does is legal, but normally, the variable
Grid1_Col_Num has external linkage, which means that all use of
the symbol refers to the same object.
mm...
StringGrid1.cpp include StringGrid1.h
StringGrid2.cpp include StringGrid2.h and StringGrid1.h
StringGrid1.h have the code "int Grid1_Col_Num = 10;"
so In StringGrid1.cpp have Grid1_Col_Num,and StringGrid2.cpp have
another one.
I don't have leave extern identifier
so...??
>
Note that without the const, Grid1_Col_Num is not a constant
expression, and cannot be used in contexts requiring a constant
expression.
And have link
warning "__Grid1_Col_Num" defined in both module.
That's ok. I know the reason.
2. Replace "#define Grid1_Col_Num 10 " with "const int Grid1_Col_Num =
10;"
Grid1_Col_Num in two cpp file have the same address. Why??

That's wrong, and a serious error if it occurs. Because of the
const, the two variables have internal linkage, and the symbol
in one translation unit does not refer to the same variable in
another translation unit.
I guess it should have different address and values are const, but
it does not.

Either it does, or your compiler is seriously broken.
3. Replace "const int Grid1_Col_Num = 10" with "static int
Grid1_Col_Num = 10;"
Grid1_Col_Num in two cpp file have the same address

Again, external linkage, and you should have an instance in each
translation unit. If not, time to change compilers.
4. Replace "const int Grid1_Col_Num = 10" with "static const int
Grid1_Col_Num = 10;"

The static is implicit when you declare const (although IMHO, it
is good practice to use). Linkage is internal, and each
translation unit gets its own instance.
Grid1_Col_Num in two cpp file have the same address
In 3 4, I think static meaning is that Grid1_Col_Num in all
application have only one copy.
Is it right?
5. so how to use const to replace define in header file????

The usual solution would be simply:

int const Grid1_Col_Num = 10 ;
int const Grid1_Rol_Num = 10 ;

in the header file.
mm..that's good for me. thank you!!
>
--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 9 '07 #4

P: n/a
On 8 9 , 11 24 , James Kanze <james.ka...@gmail.comwrote:
On Aug 9, 12:02 pm, MoslyChang <MoslyCh...@gmail.comwrote:
When I look at effective c++,item2 and item3.
I have some basic questions , Does anyone be familar with this topic?
it suggests const is perfer to #define, then I think how to
replace #define with const.
example:
2 header file StringGrid1.h StringGrid2.h
correspond to 2 implement file StringGrid1.cpp and StringGrid2.cpp
StringGrid1.cpp include StringGrid1.h
StringGrid2.cpp include StringGrid2.h and StringGrid1.h
In StringGrid1.h has the code:
#define Grid1_Col_Num 10 // represent StringGrid1 column number
#define Grid1_Rol_Num 10 // row number
StringGrid2.h has the analogue code.

What "analogue" code?
I try to replace "#define Grid1_Col_Num 10 " with something in
StringGrid1.h
belows are condition and running result
1. Replace "#define Grid1_Col_Num 10 " with "int Grid1_Col_Num = 10;"
Grid1_Col_Num in two cpp have different address.

That's wrong. You have undefined behavior, so I suppose
anything the compiler does is legal, but normally, the variable
Grid1_Col_Num has external linkage, which means that all use of
the symbol refers to the same object.

Note that without the const, Grid1_Col_Num is not a constant
expression, and cannot be used in contexts requiring a constant
expression.
And have link
warning "__Grid1_Col_Num" defined in both module.
That's ok. I know the reason.
2. Replace "#define Grid1_Col_Num 10 " with "const int Grid1_Col_Num =
10;"
Grid1_Col_Num in two cpp file have the same address. Why??

That's wrong, and a serious error if it occurs. Because of the
const, the two variables have internal linkage, and the symbol
in one translation unit does not refer to the same variable in
another translation unit.
I guess it should have different address and values are const, but
it does not.

Either it does, or your compiler is seriously broken.
3. Replace "const int Grid1_Col_Num = 10" with "static int
Grid1_Col_Num = 10;"
Grid1_Col_Num in two cpp file have the same address

Again, external linkage, and you should have an instance in each
translation unit. If not, time to change compilers.
4. Replace "const int Grid1_Col_Num = 10" with "static const int
Grid1_Col_Num = 10;"

The static is implicit when you declare const (although IMHO, it
is good practice to use). Linkage is internal, and each
translation unit gets its own instance.
Grid1_Col_Num in two cpp file have the same address
In 3 4, I think static meaning is that Grid1_Col_Num in all
application have only one copy.
Is it right?
5. so how to use const to replace define in header file????

The usual solution would be simply:

int const Grid1_Col_Num = 10 ;
int const Grid1_Rol_Num = 10 ;

in the header file.
If this suggestion is right, and you wrote that

"....., the two variables have internal linkage, and the symbol
in one translation unit does not refer to the same variable in
another translation unit."
So two translation unit have their Grid1_Col_Num1(different address).
Right???
>
--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 9 '07 #5

P: n/a

MoslyChang <Mo********@gmail.comwrote in message...
>
So two translation unit have their Grid1_Col_Num1(different address).
Right???
Look up 'extern' in your book.

--
Bob R
POVrookie
Aug 9 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.