Different instances of the same class

This is the question I should have asked WAY back in the day, when I first got an account here: Is there a way for different instances of the same class to access each other (namely each other's data)?
Short answer: No.

Long answer:
If you want to share the data between different instances of the class, then you should use static variable. Which means that it will not be a member of the object, but it will be the member of the class. For non-static variables, each object's data is its own.
Could you also use getters and setters?
Getters and setters would be called for the object.

Suppose there is a class A. And we are creating two object of this class obj1 and obj2.
If you say obj1.getvar(), the data of obj1 is returned.
You will not be able to get obj2 data by calling obj1's getter.

But you can definitely set it by calling the setter, with the data from obj1.getvar(), right? I was also trying to decide on a vector of constructors. But, because the constructors are being passed lines from a text file, declaring how many elements they should have would result in an added complexity of O(l) where l = number of lines in the file
But you can definitely set it by calling the setter, with the data from obj1.getvar()

You mean we can get the data from one object (say obj1), and set the same value to the data in obj2, by accessing the data of obj2 ? Yes, we can do that.

That is not the same as objects accessing each others data.

Let me give an example:
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
#include<iostream>
using namespace std;

class A
{
	int data;

public:
	int getdata()
	{
		return data;
	}
	void setdata(int n)
	{
		data = n;
	}
};

int main()
{
	A obj1, obj2;

	obj1.setdata(10);
	obj2.setdata(20);

	cout<<"Data of the object1 is "<<obj1.getdata()<<endl;//10
	cout<<"Data of the object2 is "<<obj2.getdata()<<endl;//20

	obj2.setdata(obj1.getdata());

	cout<<"Data of the object1 is "<<obj1.getdata()<<endl;//10
	cout<<"Data of the object2 is "<<obj2.getdata()<<endl;//10

	return 0;
}


Nowhere in this example, the data of one object is being accessed by another object.

1
2
3
4
5
6
7
class foo{
public:
   void method(foo &b){
      //here you can touch `b' as much as you like
      //because they are from the same class, you have access to its private members
   }
};
And you can definitely push stuff in obj3 that is in obj1 and obj2 (assuming they are in a heap of n>3 entries like A * aheap = new A[n])with
for (int m = 0; m < n; m++) aheap[2].setdata(aheap[m].getdata());, which would require setdata(int) to look like:
1
2
3
4
void setdata(int n)
{
      datavector.push_back(n);
}     //end setdata 


Right?
Last edited on
I answered my question by trying it myself. I decided not to delete the post, because I want it to be here for my future reference or some unenlightened coder's reference. I was taught getters and setters back in my first two computer science courses, but never used them due to me thinking they were evil. They could have saved my grade in CSCI 240 (my second class)! Now, I end up using them. Ironic, no?
Topic archived. No new replies allowed.