Jump to content
  • Advertisement
Sign in to follow this  

C++: weird const struct problem

This topic is 3481 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
foo() : Data() {}
const struct _Data {
_Data() : a(1), b(42) {}
int a;
int b;
} Data;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!