Syronex: software solutions

C++ Quiz: Answers

Answer to question 1

The four special functions that may be generated by the compiler are:

Answer to question 2

Answer to question 3

Answer to question 4

Q1:
Element #0=32
Element #1=56
Element #2=2
Element #3=3
Element #4=4
Q1:
Element #0=32
Element #1=56
Element #2=2
Element #3=3
Element #4=4
arr destroyed for Q1
arr destroyed for Q1
The default operator=() member function simply performs a "bitcopy". Thus after the assignment q2 = q1 both objects hold a pointer to the same array. The solution involves overloading operator=() and copying the array:
Qz4 &operator=(Qz4 &rh_q) {
  if (&rh_q !=  this) {  // make sure not a self assignment
    seed = rh_q.seed;
    delete arr;          // delete old array
    arr = new int[arr_l];
    for(int i=0;i<arr_l;i++) set_arr(i, rh_q.get_arr(i));
  }
  return *this;         // allow assignment chaining
}