Re: Does Liskov's principle hold also for struct?
On Jun 22, 9:05 am, DeMarcus <use_my_alias_h...@hotmail.com> wrote:
Liskov's Substitution Principle says that public inheritance must always
model "is-a" or "works-like-a". But what about behaviorless structs?
LSP, but is about sub-types of which sub-classes are a single example.
The concept of LSP has much wider applicability than just inheritance.
C++ allows many types of sub-types, the most obvious is inheritance
(nominal sub-type), but it also allows operational polymorphism
(structural sub-types). Structural sub-typing is what the users of
dynamic programming languages call "duck typing", and it appears in C+
+ in templates. The use of things like std::max or std::less in sets
and maps use structural sub-typing.
You have a choice about how to extend your environment and both of
these mechanisms are open to you. To use structural sub-types you make
the user of the environment a templated function (member or otherwise)
which takes the struct's type as an argument:
template< typename regional_environment >
void foo( regional_environment &env ) {
env.bar = whatever;
}
If you want to be able to choose the environment at runtime you have
to use sub-classing. If the environment type is only ever chosen at
compile time then you may use structural sub-typing.
Structural sub-typing often offers far more flexibility at an API
level and is far less intrusive, but in C++ it is only available
within the compiler because the types must be fully known at that
time. Sub-classes have the advantage that they offer a runtime
polymorphism mechanism, but the technique is far more intrusive.
K
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]