cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
supertype assignmet
supertype assignmet
Dec 11, 2008 at 3:21pm UTC
joksy
(4)
Hi guys I have a question for you,
let A and B two classes and B inherit from A, st B <: A
and a is type A and b is type B
why a = b is legal and b = a not??
Thanks a lot
All the best
Dec 11, 2008 at 9:59pm UTC
jsmith
(5804)
B contains everything that A has, plus some.
Therefore, B can be made into a A by discarding the "extra stuff".
But A cannot be made into a B because the compiler does not know how to initialize B's "extra stuff".
Dec 13, 2008 at 7:57am UTC
Corpus
(99)
You may want to look up "Liskov Substitution Principle" or "LSP", a fundamental in polymordhism - in essence, LSP says that any subtype can take the place of its supertype, but not vice versa.
Topic archived. No new replies allowed.