J
James
Here is my code:
#include <iostream>
struct foo
{
foo()
{
std::cout << this
<< "->foo::foo()"
<< std::endl;
}
~foo()
{
std::cout << this
<< "->foo::~foo()"
<< std::endl;
}
};
struct foo_holder
{
foo const& m_ref;
};
int main()
{
{
foo_holder fh = { foo() };
std::cout << "okay" << std::endl;
}
std::cout << std::endl;
{
foo const& ref = foo();
std::cout << "okay" << std::endl;
}
return 0;
}
Why does the const reference not properly maintain its lifetime over the
call to 'cout' when the reference is contained within a POD 'foo_holder'? I
get the following output:
0x22ff50->foo::foo()
0x22ff50->foo::~foo()
okay
0x22ff50->foo::foo()
okay
0x22ff50->foo::~foo()
Something seems terribly wrong here... Is there anyway to overcome this?
#include <iostream>
struct foo
{
foo()
{
std::cout << this
<< "->foo::foo()"
<< std::endl;
}
~foo()
{
std::cout << this
<< "->foo::~foo()"
<< std::endl;
}
};
struct foo_holder
{
foo const& m_ref;
};
int main()
{
{
foo_holder fh = { foo() };
std::cout << "okay" << std::endl;
}
std::cout << std::endl;
{
foo const& ref = foo();
std::cout << "okay" << std::endl;
}
return 0;
}
Why does the const reference not properly maintain its lifetime over the
call to 'cout' when the reference is contained within a POD 'foo_holder'? I
get the following output:
0x22ff50->foo::foo()
0x22ff50->foo::~foo()
okay
0x22ff50->foo::foo()
okay
0x22ff50->foo::~foo()
Something seems terribly wrong here... Is there anyway to overcome this?