That's awkward. And if the goal is to have different behavior
Alternatively, some trickery using casts would do.
BTW: why would you want the const method invoked?
That's a bit longer story: The array in the class will be an
array of pointers to rather large amounts of data. And I
will need lots of copies of that class. In the copies
typically only small subsets of the data will have to be
changed. Thus, to keep the total amount of memory used down,
my idea was to have boost::shared_ptr's in the array (thus
having a refe- rence count and automatic deallocation) and
to make a "real" copy of an element only when it is needed,
i.e. when a non- const instance of the element is requested
and the reference count isn't 1. For that I had hoped for
the function for re- turning a const reference/pointer to be
invoked when a const reference/pointer is requested and the
non-const returning version otherwise (in which then a copy
is made when neces- sary). And then, of course, I hoped to
make all that com- pletely transparent to the user of the
class, so they don't have to think too much about what
they're doing...
I see: you want copy-on-write. The problem is _where_ and
_how_ the copy-on- write should be handled. It is somewhat
tricky to use COW with classes that have not been designed
with that in mind. Your options include:
a) If you control the classes that your container should
contain, then have them handle the COW transparently and just
use std::vector (or some other container type from STL) as the
container. The copy will be made transparently when a
non-const member function on the retreived object is invoked.
b) Change the interface of the container so that you don't
rely on "overloading via return type". E.g.: have different
names for functions returning const references or do something
like:
void get_ptr ( T const * & p_ref, size_type index );
void get_ptr ( T * & p_ref, size_type index );
with the object.