Chapter 4 The Semantics of Function
4.1 Varieties of Member Invocation
One C++ design criterion is that a nonstatic member function at a minimum must be as efficient as its analogous nonmember function.
Steps in the transformation of a member function:
- Rewrite the signature to insert an additional argument to the member function that provides access to the invoking class object. This is called the implicit this pointer:
// non-const nonstatic member augmentation
Point3d
Point3d::magnitude( Point3d *const this )
If the member function is const, the signature becomes
// const nonstatic member augmentation
Point3d
Point3d::magnitude( const Point3d *const this )
- Rewrite each direct access of a nonstatic data member of the class to access the member through the this pointer:
{
return sqrt(
this->_x * this->_x +
this->_y * this->_y +
this->_z * this->_z );
}
- Rewrite the member function into an external function, mangling its name so that it’s lexically unique within the program:
extern magnitude__7Point3dFv(
register Point3d *const this );
Name Mangling
In general, member names are made unique by concatenating the name of the member with that of the class. For example, given the declaration
class Bar {
public: int ival; ... };
ival becomes something like
// a possible member name-mangling
ival__3Bar
class Foo : public Bar {
public: int ival; ... };
// Pseudo C++ Code
// internal representation of Foo
class Foo {
public:
int ival__3Bar;
int ival__3Foo