Click here to Skip to main content
15,910,603 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: #define vs. const Pin
Tomasz Sowinski9-Sep-02 2:39
Tomasz Sowinski9-Sep-02 2:39 
GeneralRe: #define vs. const Pin
Alexandru Savescu9-Sep-02 2:43
Alexandru Savescu9-Sep-02 2:43 
GeneralRe: #define vs. const Pin
Tomasz Sowinski9-Sep-02 2:48
Tomasz Sowinski9-Sep-02 2:48 
GeneralRe: #define vs. const Pin
Jawache9-Sep-02 3:00
Jawache9-Sep-02 3:00 
GeneralRe: #define vs. const Pin
Joaquín M López Muñoz9-Sep-02 3:30
Joaquín M López Muñoz9-Sep-02 3:30 
GeneralRe: #define vs. const Pin
Alexandru Savescu9-Sep-02 3:42
Alexandru Savescu9-Sep-02 3:42 
GeneralRe: #define vs. const Pin
Joaquín M López Muñoz9-Sep-02 3:56
Joaquín M López Muñoz9-Sep-02 3:56 
GeneralRe: #define vs. const Pin
anju10-Sep-02 21:25
anju10-Sep-02 21:25 
This is From Thinking in C++ book
------------------------------------
Constants

In old (pre-Standard) C, if you wanted to make a constant, you had to use the preprocessor:


#define PI 3.14159
Everywhere you used PI, the value 3.14159 was substituted by the preprocessor

When you use the preprocessor to create constants, you place control of those constants outside the scope of the compiler. No type checking is performed on the name PI and you can’t take the address of PI (so you can’t pass a pointer or a reference to PI). PI cannot be a variable of a user-defined type. The meaning of PI lasts from the point it is defined to the end of the file; the preprocessor doesn’t recognize scoping.


C++ introduces the concept of a named constant that is just like a variable, except that its value cannot be changed. The modifier const tells the compiler that a name represents a constant. Any data type, built-in or user-defined, may be defined as const. If you define something as const and then attempt to modify it, the compiler will generate an error.


You must specify the type of a const, like this:


const int x = 10;
In Standard C and C++, you can use a named constant in an argument list, even if the argument it fills is a pointer or a reference (i.e., you can take the address of a const). A const has a scope, just like a regular variable, so you can “hide” a const inside a function and be sure that the name will not affect the rest of the program.


The const was taken from C++ and incorporated into Standard C, albeit quite differently. In C, the compiler treats a const just like a variable that has a special tag attached that says “Don’t change me.” When you define a const in C, the compiler creates storage for it, so if you define more than one const with the same name in two different files (or put the definition in a header file), the linker will generate error messages about conflicts. The intended use of const in C is quite different from its intended use in C++ (in short, it’s nicer in C++).



------------------------------------

anju
QuestionWhy does addstring in alistbox is delayed? Pin
chen9-Sep-02 2:29
chen9-Sep-02 2:29 
AnswerRe: Why does addstring in alistbox is delayed? Pin
Tomasz Sowinski9-Sep-02 2:38
Tomasz Sowinski9-Sep-02 2:38 
GeneralRe: Why does addstring in alistbox is delayed? Pin
chen9-Sep-02 3:24
chen9-Sep-02 3:24 
GeneralRe: Why does addstring in alistbox is delayed? Pin
Tomasz Sowinski9-Sep-02 3:28
Tomasz Sowinski9-Sep-02 3:28 
GeneralRe: Why does addstring in alistbox is delayed? Pin
chen9-Sep-02 3:35
chen9-Sep-02 3:35 
GeneralRe: Why does addstring in alistbox is delayed? Pin
Tomasz Sowinski9-Sep-02 3:50
Tomasz Sowinski9-Sep-02 3:50 
GeneralRe: Why does addstring in alistbox is delayed? Pin
chen9-Sep-02 3:51
chen9-Sep-02 3:51 
GeneralCall dinamically VB ActiveX dlls Pin
Alex Gomara9-Sep-02 2:09
Alex Gomara9-Sep-02 2:09 
GeneralRe: Call dinamically VB ActiveX dlls Pin
Rama Krishna Vavilala9-Sep-02 4:46
Rama Krishna Vavilala9-Sep-02 4:46 
GeneralRe: Call dinamically VB ActiveX dlls Pin
Alex Gomara9-Sep-02 7:01
Alex Gomara9-Sep-02 7:01 
GeneralActiveX control insertion problem: "No Valid Type library" Pin
Jonathan de Halleux9-Sep-02 1:49
Jonathan de Halleux9-Sep-02 1:49 
General/Change the CEdit caret Pin
Eugene Pustovoyt9-Sep-02 1:17
Eugene Pustovoyt9-Sep-02 1:17 
GeneralRe: /Change the CEdit caret Pin
Tomasz Sowinski9-Sep-02 1:24
Tomasz Sowinski9-Sep-02 1:24 
GeneralRe: Change the CEdit caret Pin
Eugene Pustovoyt9-Sep-02 1:29
Eugene Pustovoyt9-Sep-02 1:29 
GeneralRe: Change the CEdit caret Pin
Tomasz Sowinski9-Sep-02 1:41
Tomasz Sowinski9-Sep-02 1:41 
GeneralRe: Change the CEdit caret Pin
Eugene Pustovoyt9-Sep-02 1:58
Eugene Pustovoyt9-Sep-02 1:58 
GeneralRe: Change the CEdit caret Pin
Tomasz Sowinski9-Sep-02 2:01
Tomasz Sowinski9-Sep-02 2:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.