Microsoft C++ assigns a memory model to every class, known as the “ambient” memory model for that class. The ambient model of a class affects several characteristics of the objects of that class:
The address space in which an object resides
The address mode of a pointer or a reference to an object
The address mode of the this pointer used in member functions of that class
The default ambient model for all classes is the model you specify for data at compilation. In the tiny, small, or medium memory models, all objects reside in the default data segment and all pointers and references to objects are near. In the compact, large, and huge memory models, objects can reside in any segment and all pointers and references to objects are far.
You can declare a particular class to have an ambient model other than the default by specifying __near or __far before the class name. For example, the following declaration specifies Node as a far class:
class __far Node
{
public:
Node();
void print();
~Node();
private:
// ...
};
Objects of class Node can be stored in any data segment, no matter what memory model the program is compiled with. Pointers and references to Node objects are automatically far, and a far address is passed whenever a member function is called. For example,
Node head; // Far allocation
Node *pNode; // Far pointer
head.print(); // Far address passed to print()
pNode = &head; // Far address taken
If you explicitly specify an ambient model for a class, it must match that of its base class. If a class has multiple base classes, all of them must have the same ambient model. If you don't explicitly specify the __near or __far keywords in a class's declaration, the class inherits the ambient model of its base class(es). If the class has no base class(es), it uses the default model implied by the memory model for the entire module.