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

Gabriel Dos Reis gdr@integrable-solutions.net
Sun Jan 9 07:05:00 GMT 2005


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



More information about the Gcc mailing list