Chapter 4. The Semantics of Function

If we have a Point3d pointer and object:

Point3d obj; 
Point3d *ptr = &obj; 

the question is, what actually happens when we write

obj.normalize(); 
ptr->normalize(); 

where Point3d::normalize() is implemented as

Point3d 
Point3d::normalize() const 
{ 
   register float mag = magnitude(); 
   Point3d normal; 

   normal._x = _x/mag; 
   normal._y = _y/mag; 
   normal._z = _z/mag; 

   return normal; 
} 

and Point3d::magnitude() is implemented as

float 
Point3d::magnitude() const 
{ 
   return sqrt( 
         _x * _x + _y * _y + _z * _z 
   ); 
} 

The answer is, we don’t yet know. C++ supports three flavors of member functions: static, nonstatic, and virtual. Each is invoked differently; those differences are the topic of the next section. (A short quiz: Although ...

Get Inside the C++ Object Model now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.