|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2002-09-04 15:37:04
From: "Bryan Ross" <bross_at_[hidden]>
> I may not be understanding something here, but are you saying that the
> following code won't work?
>
> #include <iostream>
> #include <string>
> using namespace std;
> struct hold
> {
> hold(const int& i) : x(i) {}
> const int& x;
> };
> int f()
> {
> return 3;
> }
> int main(int argc,char** argv)
> {
> hold* h = new hold(f());
> cout << h->x << endl;
> delete h;
> return 0;
> }
Try spaces instead of tabs next time.
> This compiles and outputs '3', as expected.
Pure luck. Try this instead:
#include <iostream>
#include <string>
static int cnt = 42;
struct X {
X() : x(cnt++) { std::cout << "X(): " << x <<std::endl;}
~X() { std::cout << "~X(): " << x <<std::endl; x = -666; }
int x;
};
using namespace std;
struct hold
{
hold(const X& i) : x(i) {}
const X& x;
};
X f()
{
return X();
}
int main(int argc,char** argv)
{
hold* h = new hold(f());
cout << h->x.x << endl;
delete h;
return 0;
}
-----------------------------------------------------------
David Abrahams * Boost Consulting
dave_at_[hidden] * http://www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk