Re: cannot recognize vector as a vector, please help

From: Malte Starostik (malte_at_starostik.de)
Date: 03/29/05


Date: Tue, 29 Mar 2005 21:48:13 +0200

John Guo schrieb:
> Thanks Chris.
>
> The only thing I want to achieve is to have an array of constant
> strings predefined. It seems to me that if I use the vector of string
> approach, I have to initialize it by calling a function. This is
> awkward. I am originally using char[][], and init it in the namespace.
>
> Could someone suggest a more elegant approach to do the same thing?

You could use an accessor function:

typedef std::vector< std::string > PatternsMsgs;

const PatternMsgs& patternMsgs()
{
    static PatternMsgs messages;
    static bool initialised = false;
    if ( !initialised )
    {
        // fill the vector
        initialised = true;
    }
    return messages;
}

that is if the messages are constant. Make it return a non-const
reference otherwise.

Cheers,
Malte