Sign in to follow this  
Castaa

C++: weird const struct problem

Recommended Posts

// 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)

Share this post


Link to post
Share on other sites
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;
};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this