Re: What's the philosophical definition of const? Organization: SunSITE.dk - Supporting Open source
On Apr 21, 6:44 pm, DeMarcus <nob...@tellus.orb.sunsite.dk> wrote:
Is there any common idea of the _philosophical_ definition of const or
is this very individual in the business? What is your opinion on this?
Being a C++ list I can say what Stroupstrup says in the TC++PL book:
"It indicates that these functions do not modify the state of a Date."
In that case Date was the class with some of its member functions
defined as "const".
I agree with Stroupstrup: const indicates to the user of the class
that calling that method not state modification occurs.
In my experience the only cases where I wrote a const method that
modified the internal state of a class were for lazy calculation.
E.g.:
--- [snip] ---
class my_class
{
private: mutable double _stats;
private: mutable bool _stats_computed;
//...
public: double stats() const
{
if (!this->_stats_computed)
{
// a very heavy-load computation
// for computing stats
this->_stats = ...
this->_stats_computed = true;
}
return this->_stats;
}
};
--- [snip] ---
To explain what I mean I give this example:
class CarFactory
{
void addCarPrototype( Car* car, string carType ) const;
void setCarTypeToManufacure( string carType );
Car* createCar() const;
}
Here addCarPrototype is a const function in my eyes. It will for sure
alter bits and bytes within CarFactory but in my philosophical meaning
it doesn't alter the object CarFactory.
From my point of view the state of CarFactory has changed: after a
call to addCarPrototype, the factory is able to create another kind of
car (i.e., the one just inserted). So its capabilities are increased.
Here is another example.
class Boat
{
void boardPassenger( Passenger* passenger ) const;
... a lot of other functions belonging to a boat ...
}
I add passengers to the boat but they don't alter my boat (hopefully).
You haven't altered your boat as shape but you have altered it in
passengers capacity: after boardPassenger the object is able to accept
only N-1 passengers (assuming its capacity was N just before the call
to boardPassenger).
So in my opinion the state of the object has changed.
Cheers,
-- Marco
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]