I can't solve this problem.
Before use cout, I can see correct values.
But after use cout, why values are changed?
Is it caused by cout?
please tell me why...
#include <iostream>
using namespace std;
/*
03_CPP_Refer.ppt p27 excercise02
*/
typedef struct Point {
int xpos;
int ypos;
} Point;
Point& pntAddr(const Point &p1, const Point &p2) {
Point rst;
rst.xpos = p1.xpos + p2.xpos;
rst.ypos = p1.ypos + p2.ypos;
return rst;
}
/*
Point struct add
*/
void ex03_02() {
Point * p1 = new Point;
Point * p2 = new Point;
p1->xpos = 3;
p1->ypos = 30;
p2->xpos = 70;
p2->ypos = 7;
Point &result = pntAddr(*p1, *p2);
cout << "[" << result.xpos << ", " << result.ypos << "]" << endl;//correct result [73, 37]
std::cout << "[" << p1->xpos << ", " << p1->ypos << "]+";
std::cout << "[" << p2->xpos << ", " << p2->ypos << "]=";
cout << "[" << result.xpos << ", " << result.ypos << "]" << endl;//incorrect result [ garbage, garbage ]
delete p1;
delete p2;
}
void main(int argc, char * argv[]) {
ex03_02();
}
output :
[73, 37] : correct value
[3, 30]+[70, 7]=[13629668, 13630652]