Victor Bazarov wrote:
kathy wrote: If in one of the header file, a constant is defined:
const UINT UM_CAL_DONE = func();
and it is included in several other .cpp file. Does the func()
excecuted many times?
Yes, very likely. To prevent that, declare your constant 'extern'
and only define (and initialise) it in a _single_ translation unit.
Just to prove Victors answer compile the following program with
g++-3.3 -g -Wall -c -o try.o try.cc
g++-3.3 -g -Wall -c -o foo.o foo.cc
g++-3.3 -g -Wall -o try try.o foo.o
try.h
=====
#ifndef TRY_H
#define TRY_H
int mkCint();
const int cInt = mkCint();
#endif
try.cc
======
#include "try.h"
#include <iostream>
int main()
{
std::cout << cInt << std::endl;
}
int mkCint()
{
std::cout << "mkCint()" << std::endl;
return 2;
}
foo.cc
======
#include "try.h"
The output is
UNIX> ./try
mkCint()
mkCint()
2
Note: the program segfaults if compiled with g++-3.4/g++-4.0 (I've just
submitted a bug report). As work-around write to stdout in mkCint().
Regards, Stephan