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 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 ...

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