M
MoslyChang
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????
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????