Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. define Vs. const

define Vs. const

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studioquestionlearning
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Ernesto D
    wrote on last edited by
    #1

    Hi all, ibe been coding for over a year now, and ibe never actually stopped to think about this (duh). if i need to define a constant value like a string, a number, etc. for my app should i: #define MY_STRING _T("this is a string") #define MY_NUMBER 100 or: const TCHAR* MY_STRING = _T("this is a string"); const int MY_NUMBER = 100; and why? needless to say, both approaches work fine, and most code ibe seen uses the #define approach, however, a book i have (Beginning Visual C++ by Ivor Horton) sais that i should use the "const" approach, and that the #define one is obsolete and should be avoided (doesnt mention why tho) Thanks!

    N M E J 4 Replies Last reply
    0
    • E Ernesto D

      Hi all, ibe been coding for over a year now, and ibe never actually stopped to think about this (duh). if i need to define a constant value like a string, a number, etc. for my app should i: #define MY_STRING _T("this is a string") #define MY_NUMBER 100 or: const TCHAR* MY_STRING = _T("this is a string"); const int MY_NUMBER = 100; and why? needless to say, both approaches work fine, and most code ibe seen uses the #define approach, however, a book i have (Beginning Visual C++ by Ivor Horton) sais that i should use the "const" approach, and that the #define one is obsolete and should be avoided (doesnt mention why tho) Thanks!

      N Offline
      N Offline
      nguyenvhn
      wrote on last edited by
      #2

      You should not consider to use #define to create a constant. #define define a macro. That mean, it will be used by preprocessor. There is not type checking here. The identifier, that appear after define keyword, exists only during preprocessing. const define a constant. That mean, it will be used by compiler. Therefore, compiler do alot base on that define.

      1 Reply Last reply
      0
      • E Ernesto D

        Hi all, ibe been coding for over a year now, and ibe never actually stopped to think about this (duh). if i need to define a constant value like a string, a number, etc. for my app should i: #define MY_STRING _T("this is a string") #define MY_NUMBER 100 or: const TCHAR* MY_STRING = _T("this is a string"); const int MY_NUMBER = 100; and why? needless to say, both approaches work fine, and most code ibe seen uses the #define approach, however, a book i have (Beginning Visual C++ by Ivor Horton) sais that i should use the "const" approach, and that the #define one is obsolete and should be avoided (doesnt mention why tho) Thanks!

        M Offline
        M Offline
        Maxwell Chen
        wrote on last edited by
        #3
        1. Regarding to the very purpose in your question, the main difference is ~ scope. A define lasts its definition till the end of the compilation module; but a const literal only lives within its scope. For example, try to compile the code below:

        void Foo()
        {
        #define MAX_SIZE 192
        // ...
        }

        // ...

        bool Bar()
        {
        #define MAX_SIZE 512 // Oops!
        int iSize = MAX_SIZE;
        }

        When your program is very large with a lot of source files, and the define(s) hide somewhere very deep in some header file(s) included by the other header files, you would find it annoying, because it needs to undef and ...... 2) There are some other reasons about avoiding macros (not related to your question). Please read Section 7.8 - Macro in the book The C++ Programming Language, 3rd by Stroustrup. "Macros are very important in C but have far fewer uses in C++. The first rule about macros is: Don't use them unless you have to. Almost every macro demonstrates a flaw in the programming language, in the program, or in the programmer. Because they rearrange the program text before the compiler proper sees it, macros are also a major problem for many programming tools. ...... " ~ Stroustrup. The section demonstrates many flaws! Maxwell Chen

        1 Reply Last reply
        0
        • E Ernesto D

          Hi all, ibe been coding for over a year now, and ibe never actually stopped to think about this (duh). if i need to define a constant value like a string, a number, etc. for my app should i: #define MY_STRING _T("this is a string") #define MY_NUMBER 100 or: const TCHAR* MY_STRING = _T("this is a string"); const int MY_NUMBER = 100; and why? needless to say, both approaches work fine, and most code ibe seen uses the #define approach, however, a book i have (Beginning Visual C++ by Ivor Horton) sais that i should use the "const" approach, and that the #define one is obsolete and should be avoided (doesnt mention why tho) Thanks!

          E Offline
          E Offline
          Ernesto D
          wrote on last edited by
          #4

          Good points, ill stick with const from now on. Thanks

          1 Reply Last reply
          0
          • E Ernesto D

            Hi all, ibe been coding for over a year now, and ibe never actually stopped to think about this (duh). if i need to define a constant value like a string, a number, etc. for my app should i: #define MY_STRING _T("this is a string") #define MY_NUMBER 100 or: const TCHAR* MY_STRING = _T("this is a string"); const int MY_NUMBER = 100; and why? needless to say, both approaches work fine, and most code ibe seen uses the #define approach, however, a book i have (Beginning Visual C++ by Ivor Horton) sais that i should use the "const" approach, and that the #define one is obsolete and should be avoided (doesnt mention why tho) Thanks!

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            Be aware that const may allocate memory. To say #define is obsolete is idiotic. It's a tool that is extremely useful. I prefer it not be used for strings, but on occasion it's the best solution for a problem. Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups