Memory-Model Modifiers and Classes

Memory-model modifiers can also be used in declaration of an ambient memory model for classes. This “ambient memory model” overrides the addressing specified in compilation options for a given class.

Syntax

class-head:
class-key rmodeloptidentifieroptbase-specopt
class-key rmodeloptclass-nameoptbase-specopt

Consider this example:

class __far CollectionOfCustomers

{

public:

CollectionOfCustomers();

int CCAdd( Customer *CNewCustomer );

Customer& CCFindName( char *Name );

};

The preceding example declares a class called CollectionOfCustomers. No matter what memory model is specified in the compilation options, objects of type CollectionOfCustomers, along with this pointers for member functions, are far unless specifically declared as near. Consider the following example:

CollectionOfCustomers *pBestCustomers;

The preceding example declares a pointer to an object whose data is addressed far (not necessarily in the default data segment). The this pointer, which is used for the member functions CollectionOfCustomers (the constructor), CCAdd, and CCFindName, is a far pointer.

The ambient memory model can be overridden in explicit declarations as follows:

CollectionOfCustomers __near *npBestCustomers;

In the preceding declaration, npBestCustomers stores its member data in the default data segment (near) even though the ambient memory model calls for far addressing. The this pointer, however, is still far, as specified by the ambient memory model.

Warning:

If a class has an ambient memory model that is specifically overridden in an object declaration, only methods with appropriate addressing are guaranteed to work.