Re: Maintaining const in Impl classes
Stefan Naewe wrote:
news.nospam.cechner@gmail.com schrieb:
Is there a well known mechanism for re-enabling the const part of the
Impl interface? I cant find any gotw addressing this issue...
Try this:
#include <iostream>
struct MyThing
{
struct Impl {
void A() const { std::cout << "Impl::A() const\n"; }
void A() { std::cout << "Impl::A()\n"; };
};
MyThing() : pImpl_( new Impl() ) { }
void A() const { std::cout << "MyThing::A() const\n"; get()->A(); }
void A() { std::cout << "MyThing::A()\n"; get()->A(); }
private:
const Impl* get() const { return pImpl_; }
Impl* get() { return pImpl_; }
Impl* pImpl_;
};
A nice first approach, but a problem is that you could easily forget to
use get() instead of pImpl_. A better idea would be to use:
template<typename T>
class pImplPtr
{
T* ptr;
public:
pImplPtr(T* p) : ptr(p) { }
~pImplPtr() { delete ptr; }
operator T*() { return ptr; }
operator const T*() const { return ptr; }
T* operator->() { return ptr; }
const T* operator->() const { return ptr; }
private:
pImplPtr(const pImplPtr&); // prevent copying
pImplPtr& operator=(const pImplPtr&); // prevent assignment
};
Then just change the declaration of pImpl_ from
Impl* pImpl_;
to
pImplPtr<Impl> pImpl_;
and every piece of code that uses pImpl_ is automatically affected.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]