const variables in a .h

B

brekehan

I am embaressed to ask such a newbie question, but hey, we all forget
things. If I want to define some constants within a namespace and
share those constants with multiple cpp files. What is the correct way
to do so?

I was under the impression I could:

// one.h
namespace somespace
{
const double tester = 0.0;
}

class SomeClass
{
//etc etc
};

// one.cpp
#include "one.h"

int main ()
{
int x = somespace::tester;
return 0;
}

// two.cpp
#include "one.h"

int y = somespace::tester;



However, I was told that when linked this compiler will effectively
make two copies of the variable tester, one for each module. What is
the correct way to use constants in a large prject?
 
J

Jim Langston

brekehan said:
I am embaressed to ask such a newbie question, but hey, we all forget
things. If I want to define some constants within a namespace and
share those constants with multiple cpp files. What is the correct way
to do so?

I was under the impression I could:

// one.h
namespace somespace
{
const double tester = 0.0;
}

class SomeClass
{
//etc etc
};

// one.cpp
#include "one.h"

int main ()
{
int x = somespace::tester;
return 0;
}

// two.cpp
#include "one.h"

int y = somespace::tester;

However, I was told that when linked this compiler will effectively
make two copies of the variable tester, one for each module. What is
the correct way to use constants in a large prject?

It is common to make it a static so that, in fact, there is one for each
module.

static const double tester = 0.0;

The static says for a constant that it is local to the compilation unit so
you won't get link errors when it finds 2 testers when trying to link.
 
P

Pete Becker

It is common to make it a static so that, in fact, there is one for each
module.

static const double tester = 0.0;

The static says for a constant that it is local to the compilation unit so
you won't get link errors when it finds 2 testers when trying to link.

Const objects have internal linkage if they're not declared 'extern',
so marking it static doesn't add anything.
 
P

Pete Becker

However, I was told that when linked this compiler will effectively
make two copies of the variable tester, one for each module.

Maybe. Each translation unit hypothetically has its own copy, although
if you don't take their addresses out can't tell.
What is
the correct way to use constants in a large prject?

Depends on what you think the problem is. If you're worried about
having a bunch of duplicate objects hanging around because that makes
your static data larger, prove it! For int constants, chances are the
compiler won't store a copy of the object, but will use the value
directly in the generated code. For more complicated objects there
might be actual data memory used, and if there's a noticeable problem
because of this, then define the object in one place as 'extern const',
and refer to it wherever you need it.
 
L

Laurent D.A.M. MENTEN

And why not:

// one.h
namespace somespace
{
extern const double tester;
}

// one.cpp
namespace somespace
{
const double tester = 0.0;
}

:wq

Jim Langston a écrit :
 
G

Gianni Mariani

Laurent said:
And why not:

// one.h
namespace somespace
{
extern const double tester;
}

// one.cpp
namespace somespace
{
const double tester = 0.0;
}

Because that eliminates a large set of possible compile time optimizations.
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top