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. use a const variable as opposed to #define - Multiple const objects created

use a const variable as opposed to #define - Multiple const objects created

Scheduled Pinned Locked Moved C / C++ / MFC
c++htmlcombeta-testingtutorial
4 Posts 3 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.
  • Y Offline
    Y Offline
    yccheok
    wrote on last edited by
    #1

    Recently, I try to replace #define with const in header file. However, there are concerns on, multiple const object will be created, if the header file is included in multiple cpp files. For example: In version.h ------------ #ifndef VERSION #define VERSION #include const std::string version("alpha_0-22"); #endif In main.cpp ------------ #include #include "version.h" extern void fun(); int main() { printf("address of version in main=%p\n", &version); fun(); getchar(); } In fun.cpp ------------ #include #include "version.h" void fun() { printf("address of version in fun=%p\n", &version); } The output of the program will be: address of version in main=00431960 address of version in fun=00431984 It seems that two copies of version string had been created if version.h is included in different cpp file scope. Now I am worry if version.h file is included in thousand of cpp files, will thousand of version string object be created?! My alternative workaround on this is, I will let version.h declare the version string and version.cpp define the version string. In version.h ------------ #ifndef VERSION #define VERSION #include extern const std::string version; #endif In version.cpp -------------- #include "version.h" const std::string version("alpha_0-22"); Again, here is my output: address of version in main=00431960 address of version in fun=00431960 It seems that the const string just be constructed one time only. I am not sure whether this is the correct workaround? Or my concern on multiple creation of const object is not an issues? Please refer to http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.7 on why I am using const instead of const. Thank you for your feedback. yccheok

    G N 2 Replies Last reply
    0
    • Y yccheok

      Recently, I try to replace #define with const in header file. However, there are concerns on, multiple const object will be created, if the header file is included in multiple cpp files. For example: In version.h ------------ #ifndef VERSION #define VERSION #include const std::string version("alpha_0-22"); #endif In main.cpp ------------ #include #include "version.h" extern void fun(); int main() { printf("address of version in main=%p\n", &version); fun(); getchar(); } In fun.cpp ------------ #include #include "version.h" void fun() { printf("address of version in fun=%p\n", &version); } The output of the program will be: address of version in main=00431960 address of version in fun=00431984 It seems that two copies of version string had been created if version.h is included in different cpp file scope. Now I am worry if version.h file is included in thousand of cpp files, will thousand of version string object be created?! My alternative workaround on this is, I will let version.h declare the version string and version.cpp define the version string. In version.h ------------ #ifndef VERSION #define VERSION #include extern const std::string version; #endif In version.cpp -------------- #include "version.h" const std::string version("alpha_0-22"); Again, here is my output: address of version in main=00431960 address of version in fun=00431960 It seems that the const string just be constructed one time only. I am not sure whether this is the correct workaround? Or my concern on multiple creation of const object is not an issues? Please refer to http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.7 on why I am using const instead of const. Thank you for your feedback. yccheok

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      Place: #pragma once at the very beginning of the header file. This instructs the compiler to only compile the header file once.

      Y 1 Reply Last reply
      0
      • G George L Jackson

        Place: #pragma once at the very beginning of the header file. This instructs the compiler to only compile the header file once.

        Y Offline
        Y Offline
        yccheok
        wrote on last edited by
        #3

        Hem... dun think that is the root cause of this problem. since #ifndef...#define pair had already served the purpose for #pragma once...

        1 Reply Last reply
        0
        • Y yccheok

          Recently, I try to replace #define with const in header file. However, there are concerns on, multiple const object will be created, if the header file is included in multiple cpp files. For example: In version.h ------------ #ifndef VERSION #define VERSION #include const std::string version("alpha_0-22"); #endif In main.cpp ------------ #include #include "version.h" extern void fun(); int main() { printf("address of version in main=%p\n", &version); fun(); getchar(); } In fun.cpp ------------ #include #include "version.h" void fun() { printf("address of version in fun=%p\n", &version); } The output of the program will be: address of version in main=00431960 address of version in fun=00431984 It seems that two copies of version string had been created if version.h is included in different cpp file scope. Now I am worry if version.h file is included in thousand of cpp files, will thousand of version string object be created?! My alternative workaround on this is, I will let version.h declare the version string and version.cpp define the version string. In version.h ------------ #ifndef VERSION #define VERSION #include extern const std::string version; #endif In version.cpp -------------- #include "version.h" const std::string version("alpha_0-22"); Again, here is my output: address of version in main=00431960 address of version in fun=00431960 It seems that the const string just be constructed one time only. I am not sure whether this is the correct workaround? Or my concern on multiple creation of const object is not an issues? Please refer to http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.7 on why I am using const instead of const. Thank you for your feedback. yccheok

          N Offline
          N Offline
          Nibu babu thomas
          wrote on last edited by
          #4

          #ifndef, #define and #endif combination works to prevent redundant inclusion in the same file. This will not work if you are including them in different .cpp files. Since in that .cpp file the macro that you are checking is not defined. Hence it creates another std::string version var. What you have to do is create a global variable by declaring it in a header file which will be included everywhere (for example stdafx.h in VC6) like wise

          extern std::string version;

          and then defining it in any one and only one of the **.cpp** files. Now you can use the variable by including the header file where you wish to use the variable. Do read the faqs[^] too.


          Nibu thomas Software Developer

          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