Connecting Tech Pros Worldwide Help | Site Map

Smart pointer template question

  #1  
Old July 22nd, 2005, 10:54 PM
Gonçalo Rodrigues
Guest
 
Posts: n/a
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?

TIA, with my best regards,
G. Rodrigues
  #2  
Old July 22nd, 2005, 10:54 PM
Victor Bazarov
Guest
 
Posts: n/a

re: Smart pointer template question


Gonçalo Rodrigues wrote:[color=blue]
> (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]

Why not let the Ref to be derived from Object too? Something like

template<typename T> class Ref : public Object {
};

Now, it's definitely single-rooted.

V
  #3  
Old July 22nd, 2005, 10:54 PM
Gonçalo Rodrigues
Guest
 
Posts: n/a

re: Smart pointer template question


On Mon, 15 Nov 2004 11:06:23 -0500, Victor Bazarov
<v.Abazarov@comAcast.net> wrote:
[color=blue]
>Gonçalo Rodrigues wrote:[color=green]
>> (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]
>
>Why not let the Ref to be derived from Object too? Something like
>
> template<typename T> class Ref : public Object {
> };
>
>Now, it's definitely single-rooted.
>
>V[/color]

For one thing the Object-rooted hierarchy and the mirror hierarchy of
smart pointers are conceptually distinct things. The Object hierarchy
is the *real thing*, the Ref hierarchy is just a *handle* on the real
thing, or, in a more platonic mood, a shadow in the wall of a cave of
the real thing. In particular, a Ref would inherit Object's
functionality/interface which doesn't make sense.

But more importantly, suppose there is a subclass of Object that
contain *Ref's* to other Object's (this is another rule I forgot to
state: any composite objects contain Ref's or smart pointers to
Object's *not* pointers). Take, for example a Tuple of Object's. A
possible definition/implementation would be

class Tuple : public Object {
private:
Ref* array;
<etc...>
}

In this way I can put any Object in a Tuple and polymorphically elbow
my way from there due to the singly-rooted hierarchy. I don't see how
I can achieve this with your solution.

TIA, best regards,
G. Rodrigues
  #4  
Old July 22nd, 2005, 10:54 PM
Howard Hinnant
Guest
 
Posts: n/a

re: Smart pointer template question


In article <b5hhp0tg6bhujpq1lttpk60rgegrqsht1r@4ax.com>,
Gon?alo Rodrigues <op73418@mail.telepac.pt> wrote:
[color=blue]
> 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]

You might want to mimic the design of boost::shared_ptr (
http://www.boost.org/libs/smart_ptr/shared_ptr.htm ) in this respect:

template<class T, class U>
shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r);

template<class T, class U>
shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r);

template<class T, class U>
shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r);

-Howard
  #5  
Old July 22nd, 2005, 10:56 PM
Michiel Salters
Guest
 
Posts: n/a

re: Smart pointer template question


Gonçalo Rodrigues <op73418@mail.telepac.pt> wrote in message news:<b5hhp0tg6bhujpq1lttpk60rgegrqsht1r@4ax.com>. ..[color=blue]
> 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. 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
  #6  
Old July 22nd, 2005, 10:56 PM
Gonçalo Rodrigues
Guest
 
Posts: n/a

re: Smart pointer template question


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.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
A smart pointer and type forwarding Ney André de Mello Zunino answers 33 November 25th, 2006 03:55 AM
Deep Copy smart pointer not requiring virtual copy constructor Nindi73@yahoo.co.uk answers 11 November 14th, 2006 02:05 PM
Smart Pointer Question Vijai Kalyan answers 3 September 7th, 2005 09:55 PM
Smart pointer dumping core and specialization question Gonçalo Rodrigues answers 0 July 22nd, 2005 11:03 PM