seccpur seccpur - 2 months ago 13
C++ Question

Declaring struct within another struct and accessing issue when declared as private

I am trying to implement a private struct or class within another struct. Case I works whereas case II is possible after declaration all member variables as public. why so? I am aware that by default all member variables/functions in struct are public and vice versa in a class definition. Now I am a bit confuse why case II don't work? Any thought?

// case I
struct impl::playlist
{
struct
{
char name_[30];
}pod_t;

};

// case II
struct impl::playlist
{
class pod_t
{
private:
char name_[30]; // not accessible by impl member func if private
};

};

Answer

Case I works whereas case II is possible after declaration all member variables as public. why so? (not accessible by impl member func if private)

Only public members are accessible outside the class. Since member function of impl is outside the class pod_t, only public members of pod_t are available.


P.S. Your case I and II are different besides the access specifier of the member. In case I you have an object by the name pod_t whose type is an unnamed class. In case II you have a class by the name pod_t and no objects of that class.