Operator Overloading
<p>I'm new to C++, this is my first week since the upgrade from fortran.  Sorry if this is a simple question, but could someone help me with operator overloading.  I have written a program which has two classes.  One object contains a vector and two scalars, the other class simply contains the first object.  In a test implementation of this code I suspect the operator overloading to be at fault.  The program tries to accomplish the following goals:</p>

<p>1)  Initialize first structure.
2)  Initialize a second structure containing the initialized first structure.  After this is imported, the value val0 = 10 is added to every element of the vector in the enclosing structure, structure2.structure1 .
3) Output structure1 and structure2 variables to compare.</p>

<p>For this simple program my output is:  </p>

<p>100<br />
100<br />
0<br />
0<br />
0 0 10<br />
1 1 11<br />
2 2 12<br />
3 3 13<br />
...</p>

<p>I was expecting:</p>

<p>100<br />
100<br />
0<br />
10<br />
0 0 10<br />
1 1 11<br />
2 2 12<br />
3 3 13<br />
...</p>

<p>Clearly my overloaded = operator is copying my vector properly, but one of the scalars?  Could someone help?</p>

<pre><code>#include &lt;iostream&gt;
using namespace std;

typedef double* doublevec;

// This first class contains a vector, a scalar N representing the size of the vector, and another scalar used for intializing the vector.
typedef class Structure1
{
 int N, vec0;
 doublevec vec;
public: 
 // Constructor and copy constructor.
 Structure1(int Nin, int vecin) : N(Nin), vec0(vecin) { vec = new double [N]; for(int i = 0; i &lt; N; i++) { vec[i] = i + vec0; } }
 Structure1(const Structure1&amp; structurein);
 // Accessor functions:
 int get_vec0() { return vec0; }
 int get_N() { return N; }
 doublevec get_vec() { return vec; }
 // Overide equivalence operator:
 Structure1&amp; operator=(const Structure1&amp; right)
 {
  //Handle Self-Assignment
  if (this == &amp;right) return *this;
  N = right.N;
  vec0 = right.vec0;
  for (int i = 0; i &lt; N; i++)
  {
   vec[i] = right.vec[i];   
  }
  return *this;
 }
 // Destructor:
 ~Structure1() { delete []vec; }
} Structure1;

Structure1::Structure1(const Structure1&amp; structurein)
{
 N = structurein.N;
 vec = new double[N];
 for(int i = 0; i &lt; N; i++)
 {
  vec[i] = structurein.vec[i];
 }
}

// This class just contains the first structure.
typedef class Structure2
 {
  Structure1 structure;
  // Mutator Function:
  void mutate_structure();
 public:  
  // Constructor:
  Structure2(const Structure1&amp; structurein) : structure(structurein) { mutate_structure(); }
  // Accessor Function:
  Structure1 get_structure() { return structure; }
  // Destructor:
  ~Structure2() {}
 } Structure2;

void Structure2::mutate_structure()
{
 int N = structure.get_N();
 Structure1 tempstruct(N,10);
 structure = tempstruct;
}

int main (int argc, char * const argv[])
{
 const int N = 100;
 Structure1 structure1(N,0);
 Structure2 structure2(structure1);

 cout &lt;&lt; structure1.get_N() &lt;&lt; endl;
 cout &lt;&lt; structure2.get_structure().get_N() &lt;&lt; endl;
 cout &lt;&lt; structure1.get_vec0() &lt;&lt; endl;
 cout &lt;&lt; structure2.get_structure().get_vec0() &lt;&lt; endl;

 for(int i = 0; i &lt; N; i++)
 {
  cout &lt;&lt; i &lt;&lt; " " &lt;&lt; structure1.get_vec()[i] &lt;&lt; " " &lt;&lt; structure2.get_structure().get_vec()[i] &lt;&lt; endl;
 }

 return 0;
}
</code></pre>
