Connecting Tech Pros Worldwide Help | Site Map

Smart pointer template question

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 09:54 PM
Gonçalo Rodrigues
Guest
 
Posts: n/a
Default Smart pointer template question

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, 09:54 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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, 09:54 PM
Gonçalo Rodrigues
Guest
 
Posts: n/a
Default 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, 09:54 PM
Howard Hinnant
Guest
 
Posts: n/a
Default 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, 09:56 PM
Michiel Salters
Guest
 
Posts: n/a
Default 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, 09:56 PM
Gonçalo Rodrigues
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.