Re: Const members vs. Assignment operator
James Kanze wrote:
johnchx2@yahoo.com wrote:
mzdude wrote:
What is the point of having const private members then?
Very little, if any, in my experience.
It depends. Consider something like:
class Customer
{
unsigned long const id ;
// ...
} ;
Customers are entity objects, with identity (and thus don't
support copy and assignment) -- each customer is identified by
means of its id.
IMHO, they make a lot of sense... just not in value type
objects, which support copy and assignment.
Ooh! Ooh! I've got a counter-example!
(Waves hand to teacher: "call on me!")
I have a templated string class for strings of known
maximum length < 255.
(Warning: this is a simplification of my class, and done
from memory; there might be bugs.)
It has a base class, which contains the maximum length
and a pointer to the character buffer. It also contains
essentially all the code to handle the strings.
The actual objects are derived classes, templated
by maximum size, and contain the character buffer.
Assignment makes sense, but it doesn't change
the maximum size (or the location or size of the
character buffer.) Characters beyond the end of the
array are, of course, discarded by assignment.
(This is perfectly reasonable in my application.)
class LWStringBase
{
public:
const unsigned char maxLength;
unsigned char length;
const char *buffer;
LWStringBase( unsigned char max, char *buf ) :
maxLength( max ), buffer( buf ) {}
void assign( LWStringBase &, const LWStringBase & );
};
template <int mx>
class LWString : public LWStringBase
{
char myBuffer[mx+1];
public:
LWString() : LWStringBase( mx, myBuffer ) {}
LWString( const LWStringBase & ); // exercise for the
reader
LWStringBase & operator=( const LWStringBase & ); // uses
assign()
};
-- Alan McKenney
<line eater fodder>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]