* asdf:
data members defined after a private label are not accessible to code
that uses the class.
You seem to have the right idea, roughly, but consider:
* What if after a "private:" I put a "public:" and some data member
declarations? Then they are certainly /after/ the "private:".
* What if the code that uses the class is in member functions of
the class itself?
So, can I define the private data members as const?
Yes. But generally you should only use const members for a class that's
designed to be non-copyable (less imprecisely: for a class whose
instances are non-copyable by design). Using a const or reference
member prevents ordinary assignment (the member can not be changed) but
not copy construction (the member can be copied to a new instance), and
since that's not entirely intuitive to all, it can be surprising and
thus lead to bugs.
So the simple answer is, to keep things simple, don't use const or
reference members.
On the other hand it can often be a good idea to disable copying, which
you can do (for client code) by declaring a private copy constructor and
a private copy assignment operator, like
class Foo
{
private:
Foo( Foo const& ); // No such.
Foo& operator=( Foo const& ); // No such.
public:
// Whatever.
};
Disabling copying -- non-copyable class -- removes problems with
e.g. copying handles or dynamically allocated objects "owned" by each
instance.
But it has problems of its own, e.g. with current C++, C++2003, it
prohibits passing a temporary as actual argument to a 'Foo const&'
formal argument.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?