Re: Treating Foo<T>* as Foo<const T>*
This is an idea and depends on what you are trying to accomplish.
Thanks all for your insights. Here is more background on what I am
interested in achieving. Perhaps it might help the discussion.
I need a custom smart pointer with special "binding" semantics as
described below :
my_ptr<int> p1 = new int(2);
my_ptr<int> p2 = p1; //Copy construction: now p1 & p2 are
permanently "bound" together.
*p2 = new int(3); // now p1 also points to 3 (as p1 and p2 are
bound). 2 is freed.
my_ptr<int> p3;
p3 = p2; // Copy assignment: p3 takes a copy of *p2. This will not
bind p3 and p2
Binding can happen only through copy construction (not copy
asignment). Since multiple my_ptr can refer to the same object via
binding, reference counting is used to manage the lifetime of the
objects.
The basic skeleton of the internal representation is as follows:
template<class T>
struct my_ptr {
ref_counted_ptr<T>* ptr;
};
template<class T>
struct ref_counted_ptr {
int ref_count;
T* obj; //points to actual obj
};
Essentially, my_ptr points to ref_counted_ptr which points to actual
obj and also stores the ref count.When two or more my_ptr are bound,
they point to the same ref_counted_ptr obj. Having a my_ptr point to a
diff object is acheived by updating the pointer inside ref_counted_ptr
which allows all bound my_ptr objects to now point to the new object.
My original questions comes from my inability to find a way to allow a
my_ptr<const T> to bind to my_ptr<T>.
Since T* can be used where a const T* is desired (same with
shared_ptr<T> & shared_ptr<const T> ). It is natural to expect similar
semantics out of my_ptr.
-Roshan
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]