Prerequisites for C++ Operator Overloading

This is a page of miscellaneous topics, not all of them new, that we need to do before operator overloading.

The “this” pointer in a member function

In a plain old (i.e., non-static) member function, the variable this is a pointer that points to the object that the member function belongs to.
In other words, this is the invisible pointer that was passed to the member function.
See the this exercise in class point.
  1. This C++ program consists of three files:
    1. obj.h
    2. obj.C
    3. main.C, main.txt
    c++ -o ~/bin/obj main.C obj.C
    ls -l ~/bin/obj
    
    obj
    i = 10
    etc.
    

External vs. internal linkage for a function

  1. This C++ program consists of two .C files,
    1. main.C, main.txt.
    2. file1.C
    c++ main.C file1.C
    ls -l a.out
    
    ./a.out
    The f in main.C
    etc.
    
    The program contains two different functions with the same name, f. Normally, this would cause c++ to give us the following error message:
    multiple definition of `f()'
    But thanks to the keyword static in the definition of the f in file1.C, the f defined in file1.C is the private, secret possesion of file1.C.
    This f will not interfere with the f defined in main.C.
  2. Exercise.
    Remove the keyword static from the above file1.C and observe the “multiple definition” error message you get from c++.
    Then replace the static.

Inline functions

  1. inline1.C, inline1.txt.
    1. The good news: the function f defined in inline1.C occupies very little memory, and takes only a microsecond to execute.
    2. The bad news: it takes a microsecond to call (i.e., go down to) f and a microsecond to return from f (i.e., come back up).
      Therefore when we call f, we spend two-thirds of our time on the road.
  2. inline2.C, inline2.txt.
    The function f in inline2.C is now an inline function.
  3. This class point has an inline constructor, an inline member function, and an inline friend function.
    It also has a constructor, member function, and friend that are not inline.
    1. point.h
    2. point.C
    3. main.C, main.txt.
    c++ main.C point.C
    ls -l a.out
    
    ./a.out
    A = (3, 4)
    etc.
    

    Compare our old class point, none of whose functions were inline:

    1. point.h
    2. point.C
    3. main.C, main.txt

Prefix increment is simple, postfix increment is complicated.

    1. increment1.C, increment1.txt.
      Use the value of the incremented variable in an output statement.
    2. increment2.C, increment2.txt.
      Use the value of the incremented variable in an assignment statement.
      In both assignment statements, the assignment operator = executes after the increment operator ++. See levels 2, 13, and 16 in the table.

Create a copy of an object by performing “pass by value”.
Avoid creating a copy of an object by performing “pass by reference”.

  1. Assume we want to create a copy of object a.
    One way to do this is by passing a by value to a function.
    Note that the object b is passed to the function without being copied.
    1. announcer.h
    2. announcer.C
    3. main3.C, main3.txt

    In the output file main3.txt, note that we create a copy of object a: the original is object number 1, and the copy (with the same name, a) is object number 3.
    But we do not create a copy of object b.

    c++ main.C announcer.C
    ls -l
    ./a.out