Passing an object with templated static array into another

This problem just seems really strange to me because it is simple yet for some reason my class cannot pass into another class. The class PASS_OBJECT has a static array (even with 1 element this doesn't work) and when I try to pass this class (after it is initialized) I seem to lose the data inside the PASS_OBJECT. Not only that but even when I declared the class OBJECT with the type of PASS_OBJECT<int> I seem to lose the integer 99. Here's the code, note that if you comment out line 89, 92 and 93 you will notice that line 90 outputs In main 2: 99 just fine but it doesn't otherwise???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>

const int size = 1;

template <class T>
class PASS_OBJECT;
template <class S>
class OBJECT
{
    private:

        S pass_object;

    public:

    OBJECT();
    ~OBJECT();
    void PASS_OBJECT_TYPE_value(S pass_object, int x, int y);
    void PASS_OBJECT_TYPE_reference(S &pass_object, int x, int y);
};
template <class S>
OBJECT<S>::OBJECT(){}

template <class S>
OBJECT<S>::~OBJECT(){}

template <class S>
void OBJECT<S>::PASS_OBJECT_TYPE_value(S pass_object, int x, int y)
{
    std::cout<<pass_object.return_data( x, y)<<std::endl;
}

template <class S>
void OBJECT<S>::PASS_OBJECT_TYPE_reference(S &pass_object, int x, int y)
{
    std::cout<<pass_object.return_data( x, y)<<std::endl;
}


template <class T>
class PASS_OBJECT
{
    private:

        static T data_array[size][size];

    public:

    PASS_OBJECT();
    ~PASS_OBJECT();
    void insert_data( int data, int x, int y);
    int return_data( int x, int y);
};
template <class T>
T PASS_OBJECT<T>::data_array[size][size];

template <class T>
PASS_OBJECT<T>::PASS_OBJECT()//constructor
{
        for(int i = 0; i < size; i++)
            for(int j = 0; j < size; j++)
                this->data_array[i][j] = 0;
}

template <class T>
PASS_OBJECT<T>::~PASS_OBJECT(){}

template <class T>
void PASS_OBJECT<T>::insert_data( int data, int x, int y)
{
    std::cout<<this->data_array[x][y]<<std::endl;
    this->data_array[x][y] = data;
    std::cout<<this->data_array[x][y]<<std::endl;
}

template <class T>
int PASS_OBJECT<T>::return_data( int x, int y)
{
    std::cout<<this->data_array[x][y]<<std::endl;
    return this->data_array[x][y];
}

int main()
{
    PASS_OBJECT<int> pass_object;
    pass_object.insert_data( 99, 0, 0);
    std::cout<<"In main 1: "<<pass_object.return_data( 0, 0)<<std::endl;

    OBJECT<PASS_OBJECT <int> > object;
    std::cout<<"In main 2: "<<pass_object.return_data( 0, 0)<<std::endl;

    object.PASS_OBJECT_TYPE_value( pass_object, 0, 0);
    object.PASS_OBJECT_TYPE_reference( pass_object, 0, 0);

    return 0;
}
Program output:
0
99
99
In main 1: 99
0
In main 2: 0
0
0
0
0


Rememeber that array you declared as static? Well that is what you are battling with.

Whenever you create a new PASS_OBJECT object, the construtor of the object will initialise everything in the static array to zero.

So with this in mind, you can now see how you overwrote the contents of the static array with the creation of the OBJECT<PASS_OBJECT<int> > object

@smac89

Ok, for some reason I'm thinking about copy constructors but nothing is copied to before <code>OBJECT<PASS_OBJECT<int> > object</code> gets made. I'm not exactly sure how to remedy this. Feel free to add some solutions though. thanks.
Topic archived. No new replies allowed.