On 13-Mar-17 4:59 PM, Rick C. Hodgin wrote:
> Why can't I do this (what is the reasoning behind the decision to
> prohibit this from occurring at compile-time)?
>
> class xyz
> {
> public:
> xyz();
> ~xyz();
>
> private:
> static char abc[] = "abc text";
> };
(Missing semicolon added – please do post real code.)
It's for the same reason that you can't do
class Oh { auto x = 3.14; };
… even though you can do
auto x = 3.14;
… in namespace scope or local scope in a function.
As far as I know that reason is that variable declarations, data member
declarations and formal function argument declarations have /evolved/,
somewhat haphazardly and independently.
> I don't understand why that would not be allowed. In my thinking,
> it would resolve to a public variable that's observable only
> through/to a class, so that its token name is in scope only there,
> and there is only one instance of that value per class, one which
> is populated at startup.
There's much that C++ could automate for you, but doesn't.
Still, what you /can/ do is quite simple, once you know about it:
#include <iostream>
using namespace std;
struct xyz
{
static constexpr auto& abc = "abc text";
};
auto main()
-> int
{
cout << xyz::abc << endl;
}
Cheers & hth.,
- Alf