Scientific Programming and Computer Architecture
Vector class defines the eponymous namespace Vector::. If we want to refer to a member function such as
norm() outside the scope of the class (the scope here is from lines 1 through 28), the name must be given as
Vector::norm().
For example, we may define the member function norm() in a compilation unit and outside the scope of the
class Vector as
double Vector::norm() const{
...
}
We have chosen to define all the member functions in Vector.hh and within the scope of the Vector class.
Before delving into the member functions, we give an example of how Vector objects are used.
#include "Vector.hh"
int main(){
Vector v(20);
for(int i=0; i < 20; i++)
v(i) = i;
Vector w1, w2;
w1.shadow(v, 0, 10);
w2.shadow(v, 10, 10);
w1.add(w2);
w1.output("w1.txt");
}
The definition Vector v(20) invokes the class constructor on line 6. This constructor will make v.data
point to 20 doubles, set v.size to 20, and set v.owner=1, making v the owner of its data.
The body of the for-loop has v(i)=i. When we say f(x) in C/C++, f() is a function being applied to x.
Here we say v(i), but v is a class object not a function being applied to i. However, thanks to operator
overloading, the compiler interprets v(i) as v.operator()(i)---in words, the member function operator() is
applied to the target v with i as the sole argument. The member function defined on line 16 is called. This member
function will look at v.data, access the ith entry, and return a reference to it. Thus, when we say v(i)=i, the ith
entry of the Vector object v gets set to i. At the end of the for-loop, the entries of v are 0, ..., 19.
At the definition Vector w1, w2;, the Vector objects w1 and w2 are created using the empty constructor
(line 5). This empty constructor sets w1.size to 0, w1.data to NULL, and w1.owner to 0, and likewise for w2.
Neither w1 nor w2 owns any data. To begin with, they are vectors of size 0.
When we say w1.shadow(v, 0, 10), the member function shadow() defined on line 12 gets invoked. Its
effect is to set w1.data to v.data and w1.size to 10. However, w1.owner remains 0 because the data is owned
by v. The Vector object w1 is a shadow of the first 10 entries of v. Likewise, after w2.shadow(v, 10, 10),
w2.data is set to v.data+10, and w2 becomes a shadow of entries 10 through 19 of v.
When we say w1.add(w2), the member function add() (line 19) is invoked with w1 as the target and w2 as
its argument. This member function verifies that its target and its argument are vectors of the same size and adds w2
to w1, entry by entry.
The final line w1.output(“w1.txt”) outputs w1 to the file w1.txt via the member function output()
(line 26). Thus, the numbers 10, 12, ..., 28 will be output to w1.txt.
The program is not done yet, however. When the class objects v, w1, and w2 go out of scope at the end of
main(), the compiler inserts calls to the destructor (line 8). The destructor is called thrice. When destroying v, the
destructor notes that v is the owner of its data and releases the memory that data points to. There is nothing to be
done to destroy w1 and w2 because they do not own their data. The destructor returns as soon as it notes that they
are not owners.
References
https://divakarvi.github.io/bk-spca/spca.html[20-1-2019 23:44:49]