Y
yancheng.cheok
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 <string>
const std::string version("alpha_0-22");
#endif
In main.cpp
------------
#include <cstdio>
#include "version.h"
extern void fun();
int main()
{
printf("address of version in main=%p\n", &version);
fun();
getchar();
}
In fun.cpp
------------
#include <cstdio>
#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 <string>
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
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 <string>
const std::string version("alpha_0-22");
#endif
In main.cpp
------------
#include <cstdio>
#include "version.h"
extern void fun();
int main()
{
printf("address of version in main=%p\n", &version);
fun();
getchar();
}
In fun.cpp
------------
#include <cstdio>
#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 <string>
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