Newbie: Inheritance & slicing problem
schveiguy at yahoo.com
Wed Jun 2 12:36:09 PDT 2010
On Wed, 02 Jun 2010 13:47:35 -0400, Larry Luther <larry.luther at dolby.com>
> I have verified the slicing problem within C++.
> The problem disappears (in accordance with your statements) if "bar"
> is declared such that it's argument is passed by reference.
> The bottom line is that C++ overcame this sufficiently to allow
> inheritance for structs.
C++ did not overcome it, as you just discovered, it still can happen.
What they did is put a band-aid on it, and said "always use the
band-aid." That is not a good solution. Plus, you must always use
references to truly solve the problem.
In D classes are always passed by reference, so the problem cannot occur.
Structs cannot have inheritance so the problem cannot occur. In D, the
problem cannot occur, so D has successfully overcome the problem.
BTW, from Day 1, C++ structs and classes are equivalent except for one
notion -- the default protection in classes is private, the default
protection in structs is public. There is no other difference. So it's
not like they waited to add inheritance to structs until they added
references to solve the slicing problem.
More information about the Digitalmars-d-learn