Basic/simple question regarding header file

K

kathy

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? If it is included in multithread application.
data in the func() must be synchonized?
 
K

kathy

My goal is to use func() to generate a constant which can be used by
many threads or functions.
 
V

Victor Bazarov

kathy said:
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.
> If it is included in multithread application.
data in the func() must be synchonized?

That's beyond the scope of this newsgroup, but probably yes.

V
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

Victor said:
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
 
M

Marco Wahl

If in one of the header file, a constant is defined:
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

Just to provide Stephans example following Victors answer:

try.h
=====

#ifndef TRY_H
#define TRY_H

int mkCint();
extern const int cInt;

#endif


try.cc
======

#include "try.h"
#include <iostream>

const int cInt = mkCint();

int main()
{
std::cout << cInt << std::endl;
}

int mkCint()
{
std::cout << "mkCint()" << std::endl;
return 2;
}


foo.cc
======

#include "try.h"


The output is
=============

$ ./try
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().

Approved for

$ g++ --version
g++ (GCC) 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9)
 
M

mark zhong

thank you
Marco Wahl said:
Just to provide Stephans example following Victors answer:

try.h
=====

#ifndef TRY_H
#define TRY_H

int mkCint();
extern const int cInt;

#endif


try.cc
======

#include "try.h"
#include <iostream>

const int cInt = mkCint();

int main()
{
std::cout << cInt << std::endl;
}

int mkCint()
{
std::cout << "mkCint()" << std::endl;
return 2;
}


foo.cc
======

#include "try.h"


The output is
=============

$ ./try
mkCint()
2


Approved for

$ g++ --version
g++ (GCC) 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top