phoganuci
phoganuci

Reputation: 5064

Operator Overloading

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:

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.

For this simple program my output is:

100
100
0
0
0 0 10
1 1 11
2 2 12
3 3 13
...

I was expecting:

100
100
0
10
0 0 10
1 1 11
2 2 12
3 3 13
...

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

#include <iostream>
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 < N; i++) { vec[i] = i + vec0; } }
 Structure1(const Structure1& structurein);
 // Accessor functions:
 int get_vec0() { return vec0; }
 int get_N() { return N; }
 doublevec get_vec() { return vec; }
 // Overide equivalence operator:
 Structure1& operator=(const Structure1& right)
 {
  //Handle Self-Assignment
  if (this == &right) return *this;
  N = right.N;
  vec0 = right.vec0;
  for (int i = 0; i < N; i++)
  {
   vec[i] = right.vec[i];   
  }
  return *this;
 }
 // Destructor:
 ~Structure1() { delete []vec; }
} Structure1;

Structure1::Structure1(const Structure1& structurein)
{
 N = structurein.N;
 vec = new double[N];
 for(int i = 0; i < 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& 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 << structure1.get_N() << endl;
 cout << structure2.get_structure().get_N() << endl;
 cout << structure1.get_vec0() << endl;
 cout << structure2.get_structure().get_vec0() << endl;

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

 return 0;
}

Upvotes: 0

Views: 299

Answers (2)

Doug
Doug

Reputation: 9110

Your copy-constructor Structure1::Structure1(const Structure1 &) doesn't copy vec0. It's not getting initialised at all, so gets whatever is in memory.

Also, you might want to check Structure1's operator=. If you assign a large vector to a small vector, then you'll potentially overflow the array in the destination. You might need to reallocate memory in operator=.

Upvotes: 0

John Weldon
John Weldon

Reputation: 40799

it looks like vec0 isn't initialized by your copy constructor...

Try modifying your copy constructor to:

Structure1::Structure1(const Structure1& structurein)
{
    N = structurein.N;
    vec = new double[N];
    for (int i = 0; i < N; i++)
    {
        vec[i] = structurein.vec[i];
    }
    // ADD THIS LINE
    vec0 = structurein.vec0;
}

Upvotes: 3

Related Questions