Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Initializing classes as POD

Status
Not open for further replies.

xwb

Programmer
Jul 11, 2002
6,828
GB
Here's a strange one that got me foxed for a while - only happens on VC6. OK on VC2003 and gcc. I don't really know whether it is legal to initialize a class as POD or not.
Code:
class Legal
{
public:
	int tom;
	int dick;
	int harry;

private:
	void DoSomething ();
};

Legal ll = { 1, 2, 3 };

int main ()
{
	return 0;
}
Comes up with error C2552: 'll' : non-aggregates cannot be initialized with initializer list
To fix it, comment out private.
 
It is legal to initialize a class (or struct) as a POD as long as the class has:
[ul]
[li]No user-declared constructors[/li]
[li]No private or protected non-static data members[/li]
[li]No base classes[/li]
[li]No virtual functions[/li]
[/ul]

I'm guessing that's a bug in VC++ 6.0 that considers the private member function to conflict with the second part in the list. The correct interpretation appears to be that only data members (members that can hold an actual value) are subject to that restriction, not member functions.

So your code should be legal according to the standard. Note that it also compiles with Comeau online.
 
Thanks for the confirmation.

I'd like the client to move to a more up to date compiler so we can at least drop the pragma warning(disable:4768) and have a better IDE but they're set on VC6 SP5 (note even SP6!) so we have to live with bugettes like these for the next 3 years. So much for state-of-the-art technology companies!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top