c++ classes as unions

I have found out that unions are classes in c++.

If you declare a class as a union:

union Foo    // Declare union type
{
    char   ch;

    int func(int a);

};          

Will the size of the union be 4 or 1 (assuming char size of 1 and pointers size of 4 ) ?

Answers


I have found out that unions are classes too in c++. (emphasis mine)

No (they are a class-type, not classes).My bad, apparently they are classes:

3.9.2/1
  • [...]
  • unions, which are classes capable of containing objects of different types at different times
  • [...]

(no longer relevant) Unions can't have virtual member functions & also can't be used in inheritance.

Onto the answer:

The size will be large enough to accommodate the largest data member. In this case, it will likely be 1, yes.


Need Your Help

Who is preventing the release of Java 1.7?

java java-7 jsr

I recently attended a talk by a Sun engineer, Charlie Hunt, regarding performance. The talk was interesting enough but one question was regarding the release date of 1.7.

How to add foreign key to entity

c# entity-framework

An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identifie...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.