On 16 Nov 2004 04:44:02 -0800,
Michiel.Salters@logicacmg.com (Michiel
Salters) wrote:
[color=blue]
>Gonçalo Rodrigues <op73418@mail.telepac.pt> wrote in message news:<b5hhp0tg6bhujpq1lttpk60rgegrqsht1r@4ax.com>. ..[color=green]
>> Hi all,
>>
>> (note: newbie alert)
>>
>> Suppose you have a hierarchy of objects that you deal with via smart
>> pointers, e.g. something like:
>>
>> template<typename T>
>> class Ref {
>> private:
>> T* ptr;
>> <etc..>
>> };
>>
>> Now suppose also that the hierarchy is singly-rooted and call the root
>> class Object. Most of the times a smart pointer to Object is all that
>> you will ever need, but there are times when you need have a smart
>> pointer to a derived class, so what is the best way to write the Ref
>> template so that it reflects the single-rootedness of the hierarchy?
>>
>> My best up to now is
>>
>> class Ref {
>> private:
>> Object* ptr;
>> <etc...>
>> };
>>
>> template<typename T>
>> class PRef : public Ref {
>> private:
>> T* ptr;
>> <etc...>
>> };
>>
>> But this is far from optimal. Any ideas?[/color]
>
>Yes. For starters, note that DerivedObject* does NOT inherit from
>Object*, even if DerivedObject inherits from Object.[/color]
Yeah, this is the real problem: mirroring the original class hierarchy
in the Ref hierarchy so that if U is a T then Ref<U> is a Ref<T>. In
particular every Ref<T> is a Ref<Object> (for T an Object of course).
Up to now, if you exclude a brief bout with Java, my programming
experience has been with dynamically typed languages where this
mirroring problem is easy because classes are themselves first-class
citizens. So I'm still getting the hang of C++ and the
statically-typed framework and all the inherent complexities of
templates and the like.
[color=blue]
>So dumb pointers
>don't follow this pattern. They probably do share an implementation,
>so the better approach might be:
>
>template <typename T>
> class PRef { // [sic]
> private: Ref ptr;
> public: T& operator*() const { return *ptr; }
>}
>
>Regards,
>Michiel Salters[/color]
Thanks. Together with Mr. Hinnant suggestion this might actually get
me somewhere.
With my best regards,
G. Rodrigues.