Re: static const char* data member
* Martin T.:
Is there no way of making a simple char* pointer constant a header
defined class member?
There is.
The most straightforward is to just wrap it in a function, like
struct Foo
{
static char const* sss() { return "xyz"; }
};
Alternatively you can use the templated constant trick to define a "real"
constant, no function, shown below.
And: What's the rationale of allowing multiple definition of global
const objects but not so for static const class members?
I've vaguely understood, from the answers I've got from various folks on the
committee, that it had to do with the order in which new features evolved.
Standardization is, as I understand it, a somewhat chaotic process, where
the
attractor states are not all ideal in all respects.
Anyways,
<code description="usage example">
namespace detail
{
CPPX_DEFINE_5_INLINE_CONSTANTS(
PropertyNames
, wchar_t const* , nameProperty , L"name"
, wchar_t const* , addressProperty , L"address"
, wchar_t const* , usernameProperty , L"username"
, wchar_t const* , passwordProperty , L"password"
, wchar_t const* , emailProperty , L"email"
);
} // detail
class ConfigureBlahBlahWhatAndSomewhatPlusDittoDialog
: public gui::ModalDialog
, private detail::PropertyNames
{
</code>
<code description="hurray! macro!">
#define CPPX_DEFINE_5_INLINE_CONSTANTS( \
className \
,type1 ,name1 ,value1 \
,type2 ,name2 ,value2 \
,type3 ,name3 ,value3 \
,type4 ,name4 ,value4 \
,type5 ,name5 ,value5 \
) \
\
template< typename Dummy > \
struct className##_ \
{ \
static type1 const name1; \
static type2 const name2; \
static type3 const name3; \
static type4 const name4; \
static type5 const name5; \
}; \
\
template< typename Dummy > \
type1 const className##_<Dummy>::name1 = value1; \
\
template< typename Dummy > \
type2 const className##_<Dummy>::name2 = value2; \
\
template< typename Dummy > \
type3 const className##_<Dummy>::name3 = value3; \
\
template< typename Dummy > \
type4 const className##_<Dummy>::name4 = value4; \
\
template< typename Dummy > \
type5 const className##_<Dummy>::name5 = value5; \
\
typedef className##_<void> className
</code>
Generalization to 4, or even just 3, 2 or 1!, constant, is left as an
exercise
for the reader.
And so is use of the Boost preprocessor library's black magic, if one
absolutely
craves a dependency on Boost. ;-)
Cheers & hth.,
- Alf
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]