*
jessica_boxer@yahoo.com:[color=blue]
> I want a standard list of strings, which I want to implement in a
> header as:
>
> // Approach 1 in .h
> namespace StdNames
> {
> const char* name1 = "Name1";
> const char* name2 = "Name2";
> // etc
> }
>
> If I put this in the header will it allocate space for name1 and name2
> everywhere I include the header[/color]
That's a quality of implementation issue.
There are workarounds where you want a guarantee, e.g., using
non-template code only,
namespace StdNames
{
inline std::vector<char const*> names()
{
static char const* values[] =
{
"Name1",
"Name2",
};
return std::vector<char const*>( values, values+2 )
}
}
where the literal constant 2 should of course be replaced by some
function or macro.
[color=blue]
> (meaning I should have a corresponding
> cpp file that actually does the assignments, and declars the above as
> extern) or does it treat these as logical constants? Is Approach 1
> above, or apporach 2 more appropriate?
>
>
> // Approach 2 in .h
> namespace StdNames
> {
> extern const char* name1;
> extern const char* name2;
> // etc
> }
>
> // And in the .cpp file
> #include "StdNames.h"
>
> namespace StdNames
> {
> const char* name1 = "Name1";
> const char* name2 = "Name2";
> // etc
> }[/color]
If you're happy to have a .cpp file then this is also perfectly OK.
One limitation is that you cannot then use such constants in compile
time expressions such as, for pointers, template instantiations.
However, the technique I sketched above does not solve that either, and
although a "simple" template solution exists the problem it solves seems
to not occur in practice... ;-)
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?