Share via


Inheritance Keywords 

Microsoft Specific

class [__single_inheritance] class-name;
class [__multiple_inheritance] class-name;
class [__virtual_inheritance] class-name;

where:

  • class-name
    The name of the class being declared.

C++ allows you to declare a pointer to a class member prior to the definition of the class. For example:

class S;
int S::*p;

In the code above, p is declared to be a pointer to integer member of class S. However, class S has not yet been defined in this code; it has only been declared. When the compiler encounters such a pointer, it must make a generalized representation of the pointer. The size of the representation is dependent on the inheritance model specified. There are four ways to specify an inheritance model to the compiler:

  • In the IDE under Pointer-to-member representation

  • At the command line using the /vmg switch

  • Using the pointers_to_members pragma

  • Using the inheritance keywords __single_inheritance, __multiple_inheritance, and __virtual_inheritance. This technique controls the inheritance model on a per-class basis.

    Note

    If you always declare a pointer to a member of a class after defining the class, you don't need to use any of these options.

Declaring a pointer to a member of a class prior to the class definition affects the size and speed of the resulting executable file. The more complex the inheritance used by a class, the greater the number of bytes required to represent a pointer to a member of the class and the larger the code required to interpret the pointer. Single inheritance is least complex, and virtual inheritance is most complex.

If the example above is changed to:

class __single_inheritance S;
int S::*p;

regardless of command-line options or pragmas, pointers to members of class S will use the smallest possible representation.

Note

The same forward declaration of a class pointer-to-member representation should occur in every translation unit that declares pointers to members of that class, and the declaration should occur before the pointers to members are declared.

END Microsoft Specific

See Also

Reference

C++ Keywords