Re: pimpl and const correctness
ThosRTanner wrote:
Question: Why does this:
class splog
{
int *wibble;
public:
void bork() const
{
* wibble = 1;
}
};
void jim(splog const&will) { will.bork(); }
compile? It seems a pretty dangerous sort of thing to do.
This is partially answered by the FAQ, it lies in the difference between
a 'T* const' and a 'T const*'. In your case, the point is simply that the
const isn't transitive from a 'T* const' to a 'T const*'.
And - what is the recommended method of not getting caught like this
(apart from running lint).
You can easily write a wrapper that makes the const transitive, for use with
PIMPLs. Otherwise, I'd suggest simply not using pointers. If it is just an
optional single object, I'd suggest using boost::optional<> for that. If
the pointer is in fact to an array, it would be better to use a container
of the standard library.
Further, since the internal const-ness of the PIMPL pretty much doesn't
matter to the user of the enclosing class this isn't a top priority. Some
even welcome the fact that they can structure the outer interface like they
want without affecting the PIMPL interface, e.g. a pure read operation on a
DB (logically const) might require handles being changed. Further, it is
just too easy to get right and is rarely affects usability.
Uli
--
Sator Laser GmbH
Gesch??ftsf??hrer: Ronald Boers, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]