T
tarmat
I'm confused by the following. I'd appreciate any advice.
//-------------------main.cpp
#include <iostream>
using namespace std;
class A
{
public:
int val;
A(int v):val(v){}
};
/////////////////////////////////////////////
class MyClass
{
private:
A& a;
public:
MyClass(A& a_):a(a_){}
int Get(){return a.val;}
};
///////////////////////////////////////////
int main()
{
MyClass mc(A(1));
cout << "\nmc.a is now " << mc.Get();
//how can mc.a be pointing to a valid integer? Isn't A(1) a
temporary?
return 0;
}
Also, if the ctor of MyClass is changed to
MyClass(A a_):a(a_){}
I get the expected result (the reference is pointing to gobbldygook).
I don't understand why, given the behavior of the first example, this
should be so.
thanks
//-------------------main.cpp
#include <iostream>
using namespace std;
class A
{
public:
int val;
A(int v):val(v){}
};
/////////////////////////////////////////////
class MyClass
{
private:
A& a;
public:
MyClass(A& a_):a(a_){}
int Get(){return a.val;}
};
///////////////////////////////////////////
int main()
{
MyClass mc(A(1));
cout << "\nmc.a is now " << mc.Get();
//how can mc.a be pointing to a valid integer? Isn't A(1) a
temporary?
return 0;
}
Also, if the ctor of MyClass is changed to
MyClass(A a_):a(a_){}
I get the expected result (the reference is pointing to gobbldygook).
I don't understand why, given the behavior of the first example, this
should be so.
thanks