This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
constness
- To: egcs at cygnus dot com
- Subject: constness
- From: dave madden <dhm at paradigm dot webvision dot com>
- Date: Wed, 3 Jun 1998 12:39:06 -0700
I'm working on a ref-counted, copy-on-write smart pointer class tree,
and I've encountered a problem with const handling. Say Ptr is the
smart pointer, with these unsurprising operator->() definitions:
class Ptr {
// ...
RealObject *operator->();
const RealObject *operator->() const;
} ;
The non-const operator->() will clone the real object and return a
pointer to the clone if more than one smart-pointer is pointing to the
object (copy-on-write).
The problem is, it looks like the operator->() selected by egcs++ and
g++ is chosen based on the constness of the smart pointer, and not the
constness of the RealObject method being invoked:
Ptr a;
const Ptr b;
a->const_function( ); // calls non-const operator->()
b->const_function( ); // calls const operator->()
Is there any way to structure the classes so that the non-const
operator->() is only called when the method to be called through
that pointer is also non-const? Is this the way the standard says
it's supposed to be?
regards,
d.