Replace #define with const???

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

Alf P. Steinbach

* MoslyChang:
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;"

You forgot 'const'.

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.

It's up to the compiler how to optimize constants (inline 'em, fold 'em,
whatever). You can't or shouldn't attempt to change them anyway. In
this case, the compiler evidently creates only one copy of the value.

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

No, that can't be correct.

They're distinct variables in different translation units.

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?

The meaning of "static" is internal linkage. I.e. an internal detail of
the translation unit, unrelated to same-named stuff in other translation
units. When you use "const" you have that by default.


5. so how to use const to replace define in header file????

Doesn't "Effective C++" give you an example?

Anyway, use your example #2.
 
B

Barry

MoslyChang said:
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]
 
J

James Kanze

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.
 
M

MoslyChang

* MoslyChang:











You forgot 'const'.
mm...I do it deliberately.
It's up to the compiler how to optimize constants (inline 'em, fold 'em,
whatever). You can't or shouldn't attempt to change them anyway. In
this case, the compiler evidently creates only one copy of the value.


No, that can't be correct.

They're distinct variables in different translation units.
Sorry, it's a typo. You are right.
The meaning of "static" is internal linkage. I.e. an internal detail of
the translation unit, unrelated to same-named stuff in other translation
units. When you use "const" you have that by default.


Doesn't "Effective C++" give you an example?

Anyway, use your example #2.
Thank you so much.^^
 
M

MoslyChang

What "analogue" code?
In StringGrid2.h
#define Grid2_Col_Num 10
#define Grid2_Col_Num 10
.....
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.


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.


Either it does, or your compiler is seriously broken.


Again, external linkage, and you should have an instance in each
translation unit. If not, time to change compilers.


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.


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!!
 
M

MoslyChang

What "analogue" code?


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.


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.


Either it does, or your compiler is seriously broken.


Again, external linkage, and you should have an instance in each
translation unit. If not, time to change compilers.


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.


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

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top