 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Old Wolf Guest
|
Posted: Mon Apr 18, 2005 9:03 am Post subject: Use of 'const' with pImpl objects |
|
|
Example:
struct FooImpl;
struct Foo
{
void bar();
void baz() const;
Foo();
~Foo();
private:
FooImpl *pImpl;
}
The 'const' qualifier on Foo::baz does not actually have any
effect, because Foo::baz (and indeed, all of the member
functions) don't modify pImpl; they only modify *pImpl.
It would be possible to make all member functions const,
or no member functions const, and have no compiler errors.
Should I eschew it completely here? Or should I continue to
apply 'const' to the same functions that a non-pImpl class
would have had them, as a means of self-documentation?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mark Van Peteghem Guest
|
Posted: Mon Apr 18, 2005 11:05 am Post subject: Re: Use of 'const' with pImpl objects |
|
|
Old Wolf wrote:
| Quote: | Example:
struct FooImpl;
struct Foo
{
void bar();
void baz() const;
Foo();
~Foo();
private:
FooImpl *pImpl;
}
The 'const' qualifier on Foo::baz does not actually have any
effect, because Foo::baz (and indeed, all of the member
functions) don't modify pImpl; they only modify *pImpl.
It would be possible to make all member functions const,
or no member functions const, and have no compiler errors.
Should I eschew it completely here? Or should I continue to
apply 'const' to the same functions that a non-pImpl class
would have had them, as a means of self-documentation?
You could use it as self-documentation, but also as documentation |
to other users of your class.
Many classes do this, e.g. std::vector has many const methods that
could actually change the contents of the vector (because these are
in a dynamically allocated array), but they don't. Many of its
methods even have both a const and a non-const version
(e.g. operator[](size_type)), where the non-const version allows
modification of the vector, while the const version doesn't.
This changes the notion of const-ness, the contents of the vector
become really part of the object.
--
Mark dot Van dot Peteghem at q-mentum dot com
http://www.q-mentum.com -- easier and more powerful unit testing
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ahti Legonkov Guest
|
Posted: Mon Apr 18, 2005 11:07 am Post subject: Re: Use of 'const' with pImpl objects |
|
|
Old Wolf wrote:
| Quote: | It would be possible to make all member functions const,
or no member functions const, and have no compiler errors.
Should I eschew it completely here? Or should I continue to
apply 'const' to the same functions that a non-pImpl class
would have had them, as a means of self-documentation?
|
You should use const the same way you would with non-pImpl. But you
can get some help from compiler using a smart pointer. Something
like this:
#include <boost/scoped_ptr.hpp>
template <typename T>
class fancy_ptr : boost::scoped_ptr<T>
{
typedef boost::scoped_ptr<T> base_t;
public:
fancy_ptr(T* p) : base_t(p) { }
~fancy_ptr() { }
// make pointee const if pointer is const
T const* operator->() const { return base_t::operator->(); }
T const& operator*() const { return base_t::operator*(); }
T* operator->() { return base_t::operator->(); }
T& operator*() { return base_t::operator*(); }
};
class Blah
{
struct Impl;
fancy_ptr<Impl> pImpl;
public:
void bar();
void baz() const;
Blah();
~Blah();
};
struct Blah::Impl { int i; };
Blah::Blah() : pImpl(new Impl()) { }
Blah::~Blah() { }
void Blah::bar()
{ pImpl->i = 9; } // OK
void Blah::baz() const
{ pImpl->i = 10; } // ERROR - pImpl is const :)
:)
--
Ahti Legonkov
leg zero at hot dot ee
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alan Griffiths Guest
|
Posted: Tue Apr 19, 2005 10:02 pm Post subject: Re: Use of 'const' with pImpl objects |
|
|
"Old Wolf" <oldwolf (AT) inspire (DOT) net.nz> wrote
| Quote: |
It would be possible to make all member functions const,
or no member functions const, and have no compiler errors.
Should I eschew it completely here? Or should I continue to
apply 'const' to the same functions that a non-pImpl class
would have had them, as a means of self-documentation?
|
Const serves a duel role - primarily it separates out the const and
not-const interfaces available to client code (which is unaffected),
and also provides some checking of implementation code.
The latter role can be reinstated by using a const qualified pointer
instead of a raw pointer. In this case grin_ptr<> seems a good fit
for your requirement:
http://www.octopull.demon.co.uk/arglib/TheGrin.html
--
Alan Griffiths
http://www.octopull.demon.co.uk/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Apr 20, 2005 9:12 am Post subject: Re: Use of 'const' with pImpl objects |
|
|
Old Wolf wrote:
| Quote: | Example:
struct FooImpl;
struct Foo
{
void bar();
void baz() const;
Foo();
~Foo();
private:
FooImpl *pImpl;
}
The 'const' qualifier on Foo::baz does not actually have any
effect, because Foo::baz (and indeed, all of the member
functions) don't modify pImpl; they only modify *pImpl.
It would be possible to make all member functions const, or no
member functions const, and have no compiler errors.
Should I eschew it completely here? Or should I continue to
apply 'const' to the same functions that a non-pImpl class
would have had them, as a means of self-documentation?
|
This is the old logical const (aka Humpty-Dumpty const)
vs. bit-wise const discussion. I believe that a consensus
exists for logical const, although some ten years back, the
discussion could be animated.
The use of const in the interface isn't just documentation.
Client code which only has a const object, or a reference to
const, cannot call the non-const functions without a const_cast.
The fact that it is you, and not the compiler, who defines
exactly what const means doesn't mean that it doesn't mean
anything. It remains an integral part of the type system.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|