Re: inheritance headache....
On Feb 2, 11:50 am, "Alf P. Steinbach" <al...@start.no> wrote:
* James Kanze:
On Feb 2, 12:35 am, "Alf P. Steinbach" <al...@start.no> wrote:
* James Kanze:
[...]
How do you know? There was nothing in the original posting to
suggest it, and it's rather the exception, and not the rule.
On the contrary, from the original posting:
* b...@blah.com:
Now somewhere, actually EVERYWHERE in this legacy code I see
statements like this;
// semi pseudo with
abstractTarget* createTarget(abstractDescription* desc)
{
// shortcut pseudo code for dynamic cast< > ()
if ( desc is concreteDesc1)
{
return new concreteTarget1();
}
else if (desc is concreteDesc2)
{
return new concreteTarget2();
}
else
{
return 0;
}
}
Notice that no information about the created objects is retained.
Hence, as I figure it, the caller has the responsibility
for destroying them.
In other words, since no information is given, you suppose
the exceptional case, rather than the usual.
No. First, there's lots of information in the example above.
Second, what's "usual" for you is, I suspect, rather different
than the norm in industry, especially for old legacy code; a
better term might be "ideal", that you're wondering why I'm
assuming the code isn't ideal...
Or maybe the reverse. It was the usual pattern, in industry, 15
years ago, but it's true that I've not seen it presented in many
recent C++ texts.
Can you really imagine the programmer who created the above,
doing registration for events in e.g. concreteTarget1's
constructor?
Well, you may have a point there:-). Using a map of factory
objects was also a usual pattern in industry 15 years ago---if
the OP is unfamiliar with it, then maybe he's unfamiliar with
the other patterns as well.
Anyway, I think we agree in principle: if the constructor
registers the object (where ever), then a raw pointer is in
order. If it doesn't, then using an auto_ptr to hold the object
until it is registered is the more or less standard convention.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34