O'Reilly logo

Inside the C++ Object Model by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 6. Runtime Semantics

Imagine we have the apparently simple expression

if ( yy == xx.getValue() ) ... 

where xx and yy are defined as

X xx; 
Y yy; 

class Y is defined as

class Y { 
public: 
   Y(); 
   ~Y(); 
   operator==( const Y& ) const; 
   // ... 
}; 

and class X is defined as

class X { 
public: 
   X(); 
   ~X(); 
   operator Y() const; 
   X getValue(); 
   // ... 
}; 

Simple stuff, right? Okay, let’s look at how the expression is handled.

First, we determine the actual instance of the equality operator that is being referenced. In this case, this resolves to the overloaded Y member instance. This is the first transformation of our expression:

// resolution of intended operator 
if ( yy.operator==( xx.getValue() )) 

Y’s equality operator requires an argument of type Y. getValue() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required