Thursday, September 19, 2019

c++ - Can a class with all private members be a POD class?



I've heard before that POD types cannot have private data -- but according to the C++0x draft I have the requirement is looser (emphasis mine):





has the same access control (Clause 11) for all non-static data members




which seems to suggest that private data is okay so long as it's all private. I don't have a copy of C++03 though to check...



Would then, WindowsApi::Uuid be a POD class?



namespace WindowsApi
{

class Uuid
{
union
{
::UUID asUuid; //Win32's UUID struct
unsigned __int64 asInt64s[2];
unsigned __int32 asInt32s[4];
};
public:
Uuid() {}

Uuid(::UUID sourceStructure) : asUuid(sourceStructure) {}
operator ::UUID() { return asUuid; }
};
}

Answer



C++03 still does not allow non-static private or protected data in POD classes. This requirement is specified in the definition of aggregate




An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).





and POD class must be an aggregate first.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...