Accessing elements from previous object in successive object read

Hi,
Just wondering about how to access elements off an object from previous object of the same type,

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
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

class DataMat
{
public:
	double element[3][4];
public:
	explicit DataMat(void);
	virtual ~DataMat(void);
	void InitializeElements(void);
	void ShowElements(void);
	void GetAverage(void);
};


DataMat::DataMat(void)	
{
	 for(int i=0;i<3;i++)
	{
	  for(int j=0;j<4;j++)
	  {
	     element[i][j]=0;
	  }	  
	}
}

DataMat::~DataMat(void)
{
   
}

void DataMat::InitializeElements(void)
{
    for(int i=0;i<3;i++)
	{
	  for(int j=0;j<4;j++)
	  {
	     element[i][j]=(int)(rand());
	  }	  
	}

}

void DataMat::ShowElements(void)
{
	this->InitializeElements();

	      for(int i=0;i<3;i++)
			{
			  for(int j=0;j<4;j++)
			  {
				  cout<<"   "<<this->element[i][j];
			  }	 
			  cout<<endl;
			}

}
int _tmain(int argc, _TCHAR* argv[])
{
	double buff[3][4];
	DataMat temp,temp_prev;
		while(_getch()!='q')
		{
			  temp.ShowElements();
			  temp_prev=temp;
			  cout<<endl;
			  cout<<"#"<<endl;
			  temp_prev.ShowElements();
			  cout<<endl<<endl;
		}
		 // _getch();
	return 0;
}



I was expecting temp_prev to show same elements as temp after being assigned by temp.

I must be missing some thing too basic?I have to take running average of 3 elements coloumn wise, have been struck with this.

Thank you
ShowElements calls InitializeElements which assigns random numbers to the internal array. ShowElements will never show the elements that exist when the function is called.
closed account (Dy7SLyTq)
it would, but then when you have temp_prev show its elements, its gonna call initializeelements which is gonna over write all of the elements with the rand() call
But how to over come this,I need to access few elements from the previous instantation. Any pointers in that direction?
closed account (Dy7SLyTq)
overload the ='s operator and have each class have a bool variable that is set to true when you do that and then when it comes to initializeelements run it only if the bool variable is set to false
Take out line 49.
64
65
DataMat temp, temp_prev;
temp.InitializeElements();
@Daleth
Accurate, Thank you
Topic archived. No new replies allowed.