LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-05-2006, 06:05 AM   #1
emarri
LQ Newbie
 
Registered: Mar 2006
Posts: 11

Rep: Reputation: 0
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
 
Old 09-05-2006, 07:38 PM   #2
lorebett
Member
 
Registered: May 2004
Location: Italy
Distribution: Ubuntu, Gentoo
Posts: 57

Rep: Reputation: 16
Post

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
 
Old 09-06-2006, 05:04 AM   #3
magnus.therning
LQ Newbie
 
Registered: Jul 2006
Location: Cambridge
Distribution: Debian
Posts: 20

Rep: Reputation: 0
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.
 
Old 09-06-2006, 05:36 AM   #4
lorebett
Member
 
Registered: May 2004
Location: Italy
Distribution: Ubuntu, Gentoo
Posts: 57

Rep: Reputation: 16
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
 
Old 09-07-2006, 02:13 AM   #5
magnus.therning
LQ Newbie
 
Registered: Jul 2006
Location: Cambridge
Distribution: Debian
Posts: 20

Rep: Reputation: 0
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.
 
Old 09-07-2006, 03:11 AM   #6
lorebett
Member
 
Registered: May 2004
Location: Italy
Distribution: Ubuntu, Gentoo
Posts: 57

Rep: Reputation: 16
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hiding global symbols in a shared/static library jineshkj Programming 16 08-30-2006 12:45 AM
const/static members in classes lucky6969b Programming 2 03-27-2006 05:43 PM
where does static and global variables stored raviksp General 0 04-28-2005 12:23 AM
global const int X = DEFINED (shows value in debugger?) abs Programming 2 09-25-2004 11:09 AM
ipchains: add static IP global ban? ifm Linux - Security 3 08-25-2002 11:49 PM


All times are GMT -5. The time now is 09:24 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration