Re: Maintaining const in Impl classes
On Aug 8, 12:45 am, news.nospam.cech...@gmail.com wrote:
Is it inevitable that when using the pImpl idiom you must throw away
the benefits of a const-able interface in your Impl class?
No. You just have to understand how const is used with pointers.
In other words, is it inevitable that the const methods on Impl
classes will never be called?
Not inevitable. You just have to ensure you are calling the methods
through a const object if you want the const member functions called.
struct MyThing {
struct Impl {
void A() const { }; // unreachable code!
void A() { };
};
MyThing() : pImpl_( new Impl() ) { };
void A() const { pImpl_->A(); };
void A() { pImpl_->A(); };
};
// ...
const MyThing thing;
thing.A(); // will always call non-const MyThing::Impl::A()
The problem you have is that in your MyThing::A() const member
function, you are effectively calling the Impl member function through
a pointer of type Impl* const. The "const" is there because
MyThing::A() is const, so it is not allowed to change the value of
any of its member variables (amongst other things). However, this type
only means you cannot modify the value held by pImpl_. It does not say
you cannot modify the object pImpl_ points to. Therefore, MyThing::A()
const can call non-const member functions of pImpl_ (bet you didn't
expect that! You are not alone.... ;) ).
What you need is to ensure you are calling the Impl member functions
through a const object. The following example shows one way to do
this:
#include <iostream>
struct MyThing
{
struct Impl
{
void A() const
{
std::cout << "Impl::A() const" << std::endl;
};
void A()
{
std::cout << "Impl::A()" << std::endl;
};
};
Impl* pImpl_;
MyThing() : pImpl_( new Impl() ) { };
void A() const
{
// Here pImpl_ is effectively of type Impl* const
// which means we cannot change the value of pImpl_
// but we can change what it points to. An easy
// workaround is to use a const reference to say
// what we really want.
const Impl& constImpl = *pImpl_;
constImpl.A();
};
void A() { pImpl_->A(); };
};
int main()
{
MyThing nonConstThing;
const MyThing constThing;
nonConstThing.A(); // prints Impl::A()
constThing.A(); // prints Impl::A() const
}
You could use a cast instead of the assignment to a const reference in
Impl::A() const, but I think the const reference is clearer and less
likely to be confusing for people who are less familiar with the two
different placements of const for a pointer type. For example, const
Impl* and Impl* const mean two different things, but many people don't
know the difference (or can't remember which one is which). Using a
const Impl& pretty much gives everyone the right idea without making
them think too hard. See the following FAQ item for more info on the
placement of const with pointer types:
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.5
--
Computational Modeling, CSIRO (CMIS)
Melbourne, Australia
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]