c++ delete object referenced by two pointers
<p>I am just curious if there is an elegant way to solve following problem in c++:</p>

<p>I have a simulator app which contains several components connected by channels. The channels may be either network channels (two instances of the app are needed) or dummy local channel. There are two interfaces: <code>IChannelIn</code> and <code>IChannelOut</code> and two corresponding variables:</p>

<pre><code>IChannelIn* in;
IChannelOut* out;
</code></pre>

<p>The <code>DummyChannel</code> is both <code>IChannelIn</code> and <code>IChannelOut</code>. It just copies input to output. There is also <code>TCPChannelIn: public IChannelIn</code> and separate <code>TCPChannelOut: public IChannelOut</code>.</p>

<p>Now, according to user's choice , I either create one <code>DummyChannel</code></p>

<pre><code>DummyChannel* d = new DummyChannel;
in = d;
out = d;
</code></pre>

<p>or two separate objects: <code>in = new TCPChannelIn; out = new TcpChannelOut</code></p>

<p>The question is:  what should the destructor do?</p>

<pre><code>~App::App()
{
    delete in;
    delete out;
}
</code></pre>

<p>ends in an error because <code>delete in;</code> deleted also the dummy channel <code>d</code> so that <code>delete out</code> deletes already deleted thing.</p>

<p>Is there an elegant way out of this?</p>
