Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

21
Dec

Unions in C++

Related Blog Items

Default access to a union is public, it can contain member functions and member data.

Other than that there are some more specialties for unions in C++.

     

  • It can’t have a static data members or a member of reference type.
  • It can’t have virtual functions.
  • It can’t be used as a base class nor it can have base class.
  • An object of a class with a construct or a destructor or a user-defined assignment operator cannot be a member of a union

Global Anonymous Unions

An anonymous union that is declared in a named namespace or in the global namespace has to be explicitly declared static. For example

static union //anonymous union in global namespace
{
int num;
char *pc;
};
namespace NS
{
static union { double d; bool b;}; //anonymous union in a named namespace
}
int main()
{
NS::d = 0.0;
num = 5;
pc = "str";
return 0;
}

 

[tags]unions, C++ unions, union basics, unions information, global anonymous unions[/tags]

 

Popularity: 4%

You need to log on to convert this article into PDF


Related Blog Items

No Comments

No comments yet.

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image