class Matrix3x3 { public: float A[3][3]; }; |
Recall how to define reference variables and use it to access components in a class:
Matrix3x3 *p; p->A[0][0]... |
This will be very important in the next discussion....
void AddNum(Matrix3x3 *p, float x) { int i, j; for (i = 0; i < 3; i = i + 1) for (j = 0; j < 3; j = j + 1) p->A[i][j] = p->A[i][j] + x; }; |
Matrix3x3 M; AddNum(&M, 4); |
You probably do not want to do the following, but you can easily fool the compiler with underhanded programming tricks, and the AddNum will add anything (which are not necessarily Matrix3x3 variables...
int i; AddNum( (Matrix3x3 *) &i, 4); |
Just compile the program, no errors !!!
 
  The million dollar question is now:
  |
class Matrix3x3 { public: float A[3][3]; }; |
void AddNum(Matrix3x3 *p, float x) { int i, j; for (i = 0; i < 3; i = i + 1) for (j = 0; j < 3; j = j + 1) p->A[i][j] = p->A[i][j] + x; }; |
class Matrix3x3 { public: float A[3][3]; void AddNum(float x) { int i, j; for (i = 1; i < 3; i = i + 1) for (j = 1; j < 3; j = j + 1) this->A[i][j] = this->A[i][j] + x; }; }; |
The reason is that every in this "exclusive set" MUST operate on one variable of the enclosing type (which is Matrix3x3).
This assumed parameter variable is accessed through the special pointer PARAMETER variable this.
(And you do NOT need to define this special parameter variable this - every "special function" has one "automatically")
In other words:
  Every function in the "exclusive set" of function has an assumed (hidden) parameter variable this. The type of the variable this is (className *) - where "className" is the name of the enclosing class   |
Matrix3x3 M; M.AddNum(4); // Invoking a member function |
This value (= the address of the variable "M") is copied into the assumed parameter variable this!!!
class Matrix3x3 { public: float A[3][3]; void AddNum(float x) { int i, j; for (i = 1; i < 3; i = i + 1) for (j = 1; j < 3; j = j + 1) A[i][j] = A[i][j] + x; }; }; |
a + b |
operator+(a, b) |
a.operator+(b) |
a + b |
operator+(a, b) or a.operator(b) |
class Matrix3x3 { public: float A[3][3]; void operator+(float x) { int i, j; for (i = 0; i < 3; i = i + 1) for (j = 0; j < 3; j = j + 1) A[i][j] = A[i][j] + x; } }; |
int main(int argc, char *argv[]) { Matrix3x3 M; ... M + 4; } |
 
f(a, b) |
a.f(b) |
operator+(a, b) |
a.operator+(b) |
This program will compile and run
This program will not compile due to ambiguity...