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???? 5 3206
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]
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Umesh |
last post by:
Do you have any answer to it? thx.
|
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;
};
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |