C++: weird const struct problem

Started by
1 comment, last by Castaa 15 years, 1 month ago
// file.h class foo { const struct _Data { int a; int b; } Data; }; --- // file.cpp const foo::_Data Data = { 1, 2 }; --- This throws an compile error: error C2248: '_Data' : cannot access private struct declared in class 'foo' How do I assign the const values of Data? (If I define "struct _Data" as public is compiles but ideally I don't want it public)
_________________________My game: Star Sonatahttp://www.starsonata.com
Advertisement
I'm not sure what exactly it is you're trying to do here. Do you want to create an object of type foo::_Data (which is what you do in the line giving you the error), or initialize the member Data in some object of tyoe foo?

First one you cannot do, unless you're in a member function of foo. Since only foo have access to its private symbols, you cannot access _Data from outside foo.

Second one is what you use the constructor for; to initialize the members.
class foo{public:	foo() : Data() {}private:	const struct _Data {        _Data() : a(1), b(42) {}        int a;        int b;    } Data;};
You are spot on. That's what I need to do. Thanks.
_________________________My game: Star Sonatahttp://www.starsonata.com

This topic is closed to new replies.

Advertisement