Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!decwrl!infopiz!lupine!rfg From: rfg@NCD.COM (Ron Guilmette) Newsgroups: comp.lang.c++ Subject: Re: Smart Pointers Aren't Enough Keywords: garbage collection, smart pointers, frustration Message-ID: <3780@lupine.NCD.COM> Date: 8 Feb 91 04:33:31 GMT References: <11707@darkstar.ucsc.edu> Organization: Network Computing Devices, Inc., Mt. View, CA Lines: 34 In article <11707@darkstar.ucsc.edu> daniel@terra.ucsc.edu (Daniel Edelson) writes: + +Case in point: + + struct node { int i; }; + + int * foo() + { + smart_pointer_to_node p = new node; + int * i = &p->i; + return i; + } + +How do we keep the node from being collected now that +the smart pointer to it has been destroyed? +What's the best course of action here? Forbid taking the +address of members of a collected object? Well... essentially... yes. Actually, "forbid" is not the correct term. Rather than a total prohibition, you could simply *encapsulate* the ability to take the address of the member called `i' within the `struct node' type. That's quite easy to do. Just make your data members (e.g. `i') private to their containing classes. You ought to be doing that anyway. -- // Ron Guilmette - C++ Entomologist // Internet: rfg@ncd.com uucp: ...uunet!lupine!rfg // Motto: If it sticks, force it. If it breaks, it needed replacing anyway.