Boost logo

Boost :

From: Bryan Ross (bross_at_[hidden])
Date: 2002-09-04 15:53:51


Well, if that isn't just darn peculiar behavior. It calls the
destructor, but doesn't actually deallocate the space for the object.

Bryan Ross
bross_at_[hidden]

-----Original Message-----
From: David Abrahams [mailto:dave_at_[hidden]]
Sent: Wednesday, September 04, 2002 1:37 PM
To: boost_at_[hidden]
Subject: Re: [boost] safe_dereference()

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

_______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk