next up previous
Next: Improvements: Range checking Up: Simple Classes: myvector.h Previous: Improvements: Overloaded assignment operator

Improvements: Code placement

Next we consider matters of style and code placement. To make the code more readable, it is conventional to put lengthy code for methods somewhere else, rather than including them inside the class declaration. For the assignment operator we have just considered, here is how we do it in the same file:



class vector{
  ...
public:
  ...

  // Assignment =
  vector& operator=(const vector &v);
};

inline vector& vector::operator=(const vector &v){
  if(size != v.size){
    cerr << "FATAL vector::operator(const vector &) size mismatch: " <<
      size << " " << v.size << "\n";
    exit(1);
  }
  for(int i = 0; i < size; i++)vec[i] = v.vec[i];
  return *this;
}

Here we moved the code outside the class declaration. But we must leave behind a prototype declaration, so we and the compiler know it is a class method. The code for the method is placed below the class declaration.

There is a new feature here:




inline vector& vector::operator=(const vector &v){

Notice that the name of the function is now vector::operator=. The prefix vector:: is necessary because we might define an assignment operator for more than one class. So as soon as the code for any method is placed outside the class declaration, the prefix identifying the class is required. In fact even when the code appears inside the vector class declaration the compiler always understands that the complete name for the method has the prefix vector::. We are simply not required to use it in that case.

Next, notice that an extra qualifier inline has been inserted. This tells the compiler that the method is not supposed to be compiled as an isolated piece of machine code. Instead the compiled machine code should be inserted in place wherever the assignment operator is used. Code that is inlined runs faster, but repeating it for every assignment increases the size of the compiled code. All methods whose code appears inside a class declaration are automatically treated as inline, so we don't need to specify it.

Whether to use inline also has implications for where we put the code for the method. Conventionally, a class declaration is placed in a header file. So for the above example, we would normally put the the main program in one file and the rest in a file named myvector.h. Then all methods whose code appears in the header file should be declared inline if they aren't already included in the class declaration. If the code for a method is so large that inlining is undesirable, its code should be placed in a separate source file. We might name it myvector.cc. None of the vector class methods in that file should be declared inline. The file myvector.cc should be compiled and linked along with the other source file(s). Failing to follow these rules may cause the linker to complain about missing or duplicate modules.


next up previous
Next: Improvements: Range checking Up: Simple Classes: myvector.h Previous: Improvements: Overloaded assignment operator
Carleton DeTar 2007-08-17