Overriding the Ambient Memory Model

You can override a class's ambient model when you declare an individual object or a pointer to an object. Place one of the addressing keywords before the identifier:

Node __near myNode;

Node __near *npNode;

myNode.print(); // Near address converted to far when print() called

npNode = &myNode; // Near address taken

In this example, myNode is an object of type Node stored in the default data segment. Taking the address of myNode produces a near pointer. Whenever a member function is invoked for myNode, the object's near address is converted to the far address that the function expects.

You can use the __near, __far, or __huge keywords when you declare an object. You can also use a __based expression, as long as you base the object on a segment constant or a segment variable. You cannot base data on a pointer, void, or the __self segment.

When you override the class's ambient model for an individual object, the address of that object has a different addressing mode from that expected by the class's member functions. In such cases, the compiler attempts to perform a type conversion on the address of the object.

In the previous example, the address of myNode is automatically converted from a near address to a far address before it is passed to the Node constructor. The same thing happens when print() is called.

However, consider the following class definition:

class __near RecArray

{

public:

RecArray( int size );

void printNames();

~RecArray();

private:

// ...

};

In this case, the declaration and statement

RecArray __far bigArray( 5000 ); // Error: constructor

// expects near address

bigArray.printNames(); // Error: printNames()

// expects near address

result in type conversion errors, because the compiler cannot convert the far address of bigArray into the near address expected by the constructor and the printNames() member function.