Operator overloading
I am new to C ++, this is my first week after updating from fortran. Sorry if this is a simple question, but someone can help me with operator overloading. I have written a program that has two classes. One object contains a vector and two scalars, another class just contains the first object. In a test implementation of this code, I suspect operator overloading is to blame. The program tries to achieve the following goals:
1) Initialize the first structure. 2) Initialize the second structure containing the initialized first structure. Upon import, the value val0 = 10 is added to each element of the vector in the enclosing structure, structure2.structure1. 3) Compare the structure variables output1 and structure2.
For this simple program, my output is:
100
100
0
0
0 0 10
1 1 11
2 2 12
3 3 13
...
I expected:
100
100
0
10
0 0 10
1 1 11
2 2 12
3 3 13
...
Obviously my overloaded = operator is copying my vector correctly, but one of the scalars? Can anyone please 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;
}
a source to share
it looks like vec0 is not initialized by your copy constructor ...
Try changing 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;
}
a source to share
Your constructor instance Structure1 :: Structure1 (const Structure1 &) does not copy vec0. It doesn't get initialized at all, so it gets everything in memory.
Alternatively, you can check the Structure1 operator =. If you are assigning a large vector to a small vector, you will potentially overflow the array at the destination. You may need to reallocate memory in the = operator.
a source to share