Problem with objects inside objects

Hi,
I'm having a problem with objects inside other objects: for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class pepe {
private:
	int x;
	string y;
	double z;

public:
	pepe(void){ x=0; y = " "; z = 0; }
	int getX() const { return x; }
	void setX(int const & a) { x = a; }
	void setY(string const & a) { y = a; }
	void setZ(double const & a) { z = a; }
	void printPepe(){ cout << x << y << z;} 
};


And another class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class pepote {
private:
	pepe mipepe;
	int x;
	string y;
	double z;

public:
	pepote(void){ x=0; y = " "; z = 0; }
	int getX() const { return x; }
	void setX(int const & a) { x = a; }
	void setY(string const & a) { y = a; }
	void setZ(double const & a) { z = a; }
	pepe getPepe() const { return mipepe; }
	void pepote::printPepote(){ cout << x << y << z;} 
};


How do I set the parameters of the object inside the class pepote, when i do this, it returns the default 0;

1
2
3
4
5
6
7
8
9
10
int main() {
pepote maspepe;
maspepe.getPepe().setX(19);
int p = maspepe.getPepe().getX();
cout << p;
string temp;
cin >> temp;
return 0;
}


I want to be able to change the parameters of the object inside the class pepote and that they stay the same so I can access them later on.
Well, you return a copy of mipepe, so setX(19) is called on a temporary object that is immediately destroyed afterwards. If you want to modify the original object, return a reference to the member or better, don't make it private in the first place.
thanks!!
Topic archived. No new replies allowed.