473,563 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replace #define with const???

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_Nu m" 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
5 3217
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_Nu m" 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
On Aug 9, 12:02 pm, MoslyChang <MoslyCh...@gma il.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_Nu m" 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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 9 '07 #3
On 8 9 , 11 24 , James Kanze <james.ka...@gm ail.comwrote:
On Aug 9, 12:02 pm, MoslyChang <MoslyCh...@gma il.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,a nd 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_Nu m" 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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 9 '07 #4
On 8 9 , 11 24 , James Kanze <james.ka...@gm ail.comwrote:
On Aug 9, 12:02 pm, MoslyChang <MoslyCh...@gma il.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_Nu m" 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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 9 '07 #5

MoslyChang <Mo********@gma il.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6975
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the delete operator with a macro. I can't replace the delete operator by using void* as the first parameter, because then my code will not be able to...
4
2243
by: Rayer | last post by:
I have got some project src, and find it out that there is no ppl using "const" to define a constance, but using #define still more. Discussing with my friend, he said 1. #define is much easier in modify values in program for several propose, and 2. const takes memory but #define not. However, most modern textbook (for example, C++ Primal Plus...
13
10994
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code below would work... string gsub(const string & sData, const string & sFrom,
5
2709
by: Michael Hiegemann | last post by:
Hello, I am unaware whether this is the right group to ask. Please point me to another forum if required. I would like to replace a Fortran function by one which is written in C. The function shall provide the same interface for the fortran code calling the library. Fortunately, this works - currently under WinNT - well for all functions...
7
20903
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem comes in when you copy a double quote from MS Word and paste it in the text box. What happens is the double quote becomes slanted (“) so my code above...
18
4603
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also has some different characters in it that I am trying to remove. The small a with two dots over it and the small y with two dots over it. Here is my...
4
1791
by: Marcelo | last post by:
Dear all, I am a beginner of C/C++ programmation and I don't understand how the #define command works. I would like to make this statement #define MAX2(V1, V2) (V1>V2 ? V1 : V2) as a C routine in order to use it in other C programs. However, I don't know what type to put for the retourning value.
18
2407
by: Umesh | last post by:
Do you have any answer to it? thx.
23
3888
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.