J
Joe Hesse
/*****
When I run the following program (gnu C++) the address of the automatic
object 'a'
in f() is the same as the address of the automatic object 'b' in main().
I conclude that 'a' in f() is the same object as 'b' in main().
Is this an error or an optimization?
Thank you,
Joe Hesse
*****/
#include <iostream>
using namespace std;
class X {
private:
int x;
public:
X(int a = 0) : x(a) {};
};
// function returns an X object by value
X f() {
X a;
cout << "&a in f() is " << &a << endl;
return a;
}
int main() {
X b = f();
cout << "&b in main() is " << &b << endl;
return 0;
}
When I run the following program (gnu C++) the address of the automatic
object 'a'
in f() is the same as the address of the automatic object 'b' in main().
I conclude that 'a' in f() is the same object as 'b' in main().
Is this an error or an optimization?
Thank you,
Joe Hesse
*****/
#include <iostream>
using namespace std;
class X {
private:
int x;
public:
X(int a = 0) : x(a) {};
};
// function returns an X object by value
X f() {
X a;
cout << "&a in f() is " << &a << endl;
return a;
}
int main() {
X b = f();
cout << "&b in main() is " << &b << endl;
return 0;
}