Thanks to all that responded to my previous thread. There is still a problem with this simple program that I would like to solve. I am trying to import one class into another as a member object. The output of this program is confusing, though. As a test of the code, I output the scalar contained within the first class object. This works. Then I output the scalar the is contained in the object contained as a member of the second class, and this outputs zero. I assume that something is wrong. Could someone shed some light on this problem? Thanks!
#include <iostream>
using namespace std;
typedef double* doublevec;
// This first class contains a vector and a scalar representing the size of the vector.
typedef class Structure1
{
int N;
doublevec vec;
public:
// Constructor and copy constructor.
Structure1(int Nin);
Structure1(const Structure1& structurein);
// Accessor functions:
int get_N() { return N; }
doublevec get_vec() { return vec; }
// Destructor:
~Structure1() { delete []vec; }
} Structure1;
Structure1::Structure1(int Nin)
{
N = Nin;
vec = new double [N];
for (int i = 0; i < N; i++)
{
vec[i] = i;
};
}
Structure1::Structure1(const Structure1& structurein)
{
vec = new double[structurein.N];
for(int i = 0; i < structurein.N; i++)
{
vec[i] = structurein.vec[i];
};
}
// This class just contains the first structure.
typedef class Structure2
{
Structure1 structure;
public:
// Constructor:
Structure2(const Structure1& structurein) : structure(structurein) {}
// Accessor Function:
Structure1 get_structure() { return structure; }
// Destructor:
~Structure2() {}
} Structure2;
int main (int argc, char * const argv[])
{
const int N = 100;
Structure1 structure1(N);
Structure2 structure2(structure1);
cout << structure1.get_N() << endl;
cout << structure2.get_structure().get_N();
return 0;
}