Igor
Tue Sep 19 22:12:04 CDT 2006
"Darwin" <codecopier@gmail.com> wrote in message
news:1158719630.018012.219550@i3g2000cwc.googlegroups.com
> class c{
> int i_;
> public:
> c(int i){
> printf("c\r\n"); i_ = i;
> }
> ~c(){
> printf("~c\r\n"); i_ = -1;
> }
> void foo() const{
> printf("foo,%d", i_);
> }
> };
> class a{
> public:
> const c & c_;
> a() : c_( c(1) )
> {
> }
> };
>
> int main()
> {
> a _1;
> _1.c_.foo();
> }
>
> The ~c() will never invoke.
> So RAII is a dream.
It should be noted that your program exhibits undefined behavior. It
binds c_ reference to a temporary that is destroyed at the end of a's
constructor. You then call a method on a dangling reference. From C++
standard:
12.2/5 ... A temporary bound to a reference member in a constructor's
ctor-initializer (12.6.2) persists until the constructor exits...
Nevertheless, the destructor should have run, and it does not appear to,
so that definitely looks like a bug. I don't believe it's as dramatic as
you make it to be - your example is rather contrived and unlikely to
occur in practice (why would anybody want to have dangling reference as
a member?). Anyway, you can submit the bug here:
http://connect.microsoft.com/feedback/default.aspx?SiteID=210
If and when you do, post a reference to the bug here so others can
validate it.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925