LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   global static const - c++ (https://www.linuxquestions.org/questions/programming-9/global-static-const-c-480561/)

emarri 09-05-2006 06:05 AM

global static const - c++
 
hi

I'm trying to get to a the bottom of how things work with globally defined static variables.
I define a .h file with
Code:

namespace outerspace
{

static const std::string FOO_STRING        = "foo";
static const std::string BAR_STRING        = "bar";
}

Now, I want to figure out when including this header file in several other classes will those strings be replicated or will all the objects linked together be sharing the same memory where those strings are defined?

My main issue is regarding the memory usage, is there a single memory being located for all the object files including this header file or will it be replicated each time?

Hope you can assist
regards
Marri

lorebett 09-05-2006 07:38 PM

I don't think they're replicated, but I'd go for the following solution, where, I'm sure, they're not replicated:

Code:

class outerspace
{
static const std::string FOO_STRING        = "foo";
static const std::string BAR_STRING        = "bar";
}

i.e., use class static fields instead of namespaces

magnus.therning 09-06-2006 05:04 AM

they are replicated
 
The strings are in fact replicated.

I would suggest the following.

const.h
Code:

namespace foo
{
    extern const std::string foo;
    extern const std::string bar;
}

const.cc
Code:

#include "const.h"

namespace foo
{
    const std::string foo = "foo";
    const std::string bar = "bar";
}

Note that the meaning of static is different in C and C++. In C++ you should only really use it inside functions and classes. Use unnamed namespaces to make names local to a compilation unit.

lorebett 09-06-2006 05:36 AM

Isn't

Code:

class outerspace
{
static const std::string FOO_STRING        = "foo";
static const std::string BAR_STRING        = "bar";
}

more object oriented?

and it does not require to split declaration and definition in two files...

moreover you access, e.g., FOO_STRING in the same way as for namespaces... outerspace::FOO_STRING

magnus.therning 09-07-2006 02:13 AM

Quote:

Originally Posted by lorebett
Isn't

Code:

class outerspace
{
static const std::string FOO_STRING        = "foo";
static const std::string BAR_STRING        = "bar";
}

more object oriented?

and it does not require to split declaration and definition in two files...

moreover you access, e.g., FOO_STRING in the same way as for namespaces... outerspace::FOO_STRING

First, have you tried sticking the code above into a header file, including that header file in two source files, then compile them and link them into a program?

When I did that g++ complained like crazy.

Code:

const.h:6: error: invalid in-class initialization of static data member of non-integral type 'const std::string'
The way I solved it was by sticking the definition of the constants into a source file. So, I ended up splitting declaration and definition anyway.

Second, I'd argue there's nothing object oriented with global constants. Personally I stick global constants in a class, like you do above, when those constants are related to a class. If the global constants have nothing to do with a class then I stick them in a well named namespace.

I think that a few years ago, before namespaces were properly supported in all major C++ compilers, it was common to "mis-use" classes to hold global constants. This was just to avoid polluting the global namespace. Nowadays namespaces are supported and can be used instead.

lorebett 09-07-2006 03:11 AM

Quote:

Originally Posted by magnus.therning
The way I solved it was by sticking the definition of the constants into a source file. So, I ended up splitting declaration and definition anyway.

oopss... sorry you're right, I hadn't thought about this issue :eek:


All times are GMT -5. The time now is 03:50 AM.