Re: why no struct inheritance?

Tech-Archive recommends: Fix windows errors by optimizing your registry



not_a_commie <notacommie@xxxxxxxxx> wrote:
What's the technical reason for not allowing structs to be
hierarchical?

Well, it would be reasonably feasible to allow inheritance so long as
you didn't want polymorphic methods and didn't add any member
variables. You see, if I declare:

struct Point
{
int x;
int y;

// Other operations
}

and then elsewhere in your code:

Point point;

then the "point" variable has room for 8 bytes - 4 for x, 4 for y. If
we were able to do:

Point point = new ExtendedPoint();

then:
a) the runtime couldn't know the actual type of the value, because
there isn't any room for it
b) there wouldn't be room for any extra member variables of
ExtendedPoint


I'm sure there would be ways round this is value type inheritance had
been desired from the start, but the current design doesn't allow it at
all.

As Bob said, extension methods in C# 3 may well help you.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: Equvalent of "Shadows" in VB.Net?
    ... anything to do with my control, in a correct way, my control is for a ... One of the facts of life and inheritance. ... > member by defining a new member that can have a different access level, ... > protected override void OnBackColorChanged ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# Pattern for shadowing member variables?
    ... Name hiding through inheritance occurs when classes or structs redeclare ... A method introduced in a class or struct hides all non-method base class ... A declaration of a new member hides an inherited member only within the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: inheritance or member of the class
    ... > define class B is base class of class A or class B is member of class ... Public inheritance is generally used to represent an "IS-A" ... class Car: public Vehicule ...
    (microsoft.public.vc.language)
  • Re: Equvalent of "Shadows" in VB.Net?
    ... One of the facts of life and inheritance. ... The Shadows keyword in VB allows you to hide a base class ... member by defining a new member that can have a different access level, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: STL string inheritance
    ... > I want to create a data structure and a correcponding mask data structure, ... > additional functionality, specialised to the type of data they're holding, ... no virtual member functions. ... I tend to gripe about public inheritance of standard containers because it ...
    (comp.lang.cpp)