This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: [MAC] 'static const std::string' in a header file
- From: Gabriel Dos Reis <gdr at integrable-solutions dot net>
- To: Mathieu Malaterre <mmalater at nycap dot rr dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: 09 Jan 2005 01:41:48 +0100
- Subject: Re: [MAC] 'static const std::string' in a header file
- Organization: Integrable Solutions
- References: <41E06FE9.4030601@nycap.rr.com>
Mathieu Malaterre <mmalater@nycap.rr.com> writes:
| Hello,
|
| I am working on a cross plateform DICOM lib which seems to be
| working fairly well on a variety of plateforms except on MacOSX with
| gcc.
This is more a C++ programming question than GCC development question;
you might want to consult general newsgroups like
news:comp.lang.c++.moderated where you may get feedback from more experts.
|
| If I try to run any example it gets stuck on a string
| construction. I believe this has to do with multi thread and c++
| object initialization on the Mac. Could someone comment on the usage
| in a header file of:
|
| static const std::string foo = "bar";
|
| Is this a bad practice ?
I regard objects defined in header files almost always as bad ideas to
begin with. Notable exceptions are manifest constants. Why don't you
move that foo to an implementation file and encapsulate it in a function?
// header.H
const std::string& foo();
// implementation.C
const std::string& foo() {
static const std::string instance = "foo";
return instance;
}
see the abundant literature.
-- Gaby