Tuesday, November 16, 2010

Struct

The following code compiles:

   struct MyStruct {int i;};

   MyStruct s1;
   struct MyStruct s2;

Previously in C, you always had to specify the struct keyword unless you placed the declaration in a typedef. In C++, you can do it either way. classes, unions, and structs are known as "class types". The don't need their class type keywords in ordered to be used. Assuming that the corresponding class types were defined, the following are valid C++:
   struct MyStruct s1;
      MyStruct s2;
   union MyUnion u1;
      MyUnion u2;
   class MyClass c1;
      MyClass c2;
Note that a union cannot be used as a base type or derived from a base type.

No comments:

Post a Comment