By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Design of pointer_to<T> templates

P: n/a
Hi,
I have determined that replacing T* by pointer_to<T> is a good idea:

(a) If the template signature is actually

template < typename T, typename Alloc=some_standard_allocator<T> >
class pointer_to {
...
};

then the template makes it easy to play with allocators for performance
tuning or for implemeting container classes that conform to the allocator
conventions of the standard library.

(b) Using magic allocators or a different implementation of pointer_to<T>,
one can hunt for memory leaks.

(c) In code like

class B { ... };
class D : public B { ... };

pointer_to<B> b_ptr;
pointer_to<D> d_ptr;

b_ptr = d_ptr;

the compiler can issue an error if class B is not polymorphic.
It is the last point that gives me some design problems. I am considering
the following options:

I) Supply two templates:

typebound_ptr<T,Alloc>
polymorphic_ptr<T,Alloc>

where the first will not allow for polymorphic use and the second will not
compile if T is not polymorphic.

The consequence is that I will have to make up my mind every time as to
whether I intend to use a pointer type polymorphically or not. This may be
an advantage in that it probably improves code quality in most cases.
Sometimes, however, it might not be feasibile to decide beforehand -- I am
thinking of coding templates for instance.

II) Implement pointer_to<T> so that assignment, copy constructor, and
perhaps comparison operators will check whether T is polymorphic if their
polymorphic versions are used.

III) Provide all three pointer templates.

This might allow for maximum flexibilty or for maximum confusion. Moreover,
one might end up converting between these pointer types, which somewhat
defies the purpose.
I have working code for most of this, I am just about to decide which files
to ditch.
Any ideas and advice will be highly appreciated.
Best

Kai-Uwe Bux
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Kai-Uwe Bux <jk********@gmx.net> wrote:
I have determined that replacing T* by pointer_to<T> is a good idea:

Any ideas and advice will be highly appreciated.


Use loki::SmartPtr
<http://sourceforge.net/projects/loki-lib/>
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.