<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2448.0">
<TITLE>RE: [LC++]Big objects in a class interface: good techniques?</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>> I could go:</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> ...</FONT>
<BR><FONT SIZE=2>> Wiring const& wiring() const {return wiringVt;}</FONT>
<BR><FONT SIZE=2>> ...</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> but then this has memory management problems. It is giving out the</FONT>
<BR><FONT SIZE=2>> address of an internal object. What if this address becomes invalid?</FONT>
<BR><FONT SIZE=2>> The interface is no longer a safe one.</FONT>
</P>
<P><FONT SIZE=2>You can allocate Wiring as a dynamic object:</FONT>
</P>
<P><FONT SIZE=2>Light::Light(): wiring(new Wiring()) {}</FONT>
<BR><FONT SIZE=2>Light::~Light() {delete wiring;}</FONT>
</P>
<P><FONT SIZE=2>and then:</FONT>
</P>
<P><FONT SIZE=2>struct Wiring: public Refcounted {</FONT>
<BR><FONT SIZE=2>};</FONT>
</P>
<P><FONT SIZE=2>struct Refcounted {</FONT>
<BR><FONT SIZE=2>protected:</FONT>
<BR><FONT SIZE=2> Refcounted(): refs(1) {}</FONT>
<BR><FONT SIZE=2> virtual ~Refcounted() {if (!--refs) delete this;}</FONT>
<BR><FONT SIZE=2>private:</FONT>
<BR><FONT SIZE=2> size_t refs;</FONT>
<BR><FONT SIZE=2>};</FONT>
</P>
<P><FONT SIZE=2>This way your wiring object won't get destroyed if it's still</FONT>
<BR><FONT SIZE=2>used when the light is deleted. Refcounted can be a little tricky,</FONT>
<BR><FONT SIZE=2>you have to handle assignment operators, etc. It can also be a</FONT>
<BR><FONT SIZE=2>bit inefficient if you call these a lot.</FONT>
</P>
<P><FONT SIZE=2>-- </FONT>
<BR><FONT SIZE=2>Vincent Penquerc'h </FONT>
</P>
</BODY>
</HTML>