This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [MAC] 'static const std::string' in a header file


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]