473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

why no struct inheritance?

What's the technical reason for not allowing structs to be
hierarchical? WPF has a nice Point3D struct. I wanted to add some
additional constructors to it for converting from my data types.

May 6 '07 #1
12 7252
C# extension methods may be at least a partial solution for you, if you
can live with using C# 3.0, which won't be to production bits for a few
months yet:

http://bobondevelopment.com/2007/04/...nsion-methods/

That won't allow you to add a constructor, but it will allow you to
extend existing classes (including those you don't have source to) in
other ways.

In the meantime, perhaps a static helper method to convert your
arguments and return the desired struct will serve just as well.

--Bob

not_a_commie wrote:
What's the technical reason for not allowing structs to be
hierarchical? WPF has a nice Point3D struct. I wanted to add some
additional constructors to it for converting from my data types.
May 6 '07 #2
not_a_commie <no********@gmail.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 6 '07 #3
Could you just provide a cast definition from your new types?

Marc

May 6 '07 #4
Could you just provide a cast definition from your new types?

I like it. It's not clean or anything, but it should do just what I
want.

May 8 '07 #5
Am Sun, 6 May 2007 16:28:09 +0100 schrieb Jon Skeet [C# MVP]:
>
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
If this would compile (i.e no syntax error), the expected behavior is
crystal clear:
1. an object ob type ExtendedPoint is created (memory allocated + fully
constructed)
2. Memory for an object of type Point is allocated
3. The assignment is performed.

Note that we have value types, not reference types. That means: it all
depends on the definition of the assignment operator. Since we cannot
overload the assignment operator, your statement is illegal for any user
defined structs Point and ExtendedPoint.

No argument against derivation of structs.
Paule
May 12 '07 #6
Paul Werkowitz <ne********@primaprogramm.dewrote:

<snip>
Note that we have value types, not reference types. That means: it all
depends on the definition of the assignment operator. Since we cannot
overload the assignment operator, your statement is illegal for any user
defined structs Point and ExtendedPoint.

No argument against derivation of structs.
So there'd be no polymorphism possible... sounds like a good argument
against derivation of structs to me. I wouldn't be able to pass an
ExtendedPoint to a method requiring a Point, contrary to all normal
expectations. To not have a conversion available from a derived type to
the base type seems very odd to me.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 12 '07 #7
So there'd be no polymorphism possible... sounds like a good argument
against derivation of structs to me.
Value types are not made for polymorphism, virtual functions etc. For
virtual functions to work, you need the concept of a handle, pointer,
reference etc. You need to be able to distinguish between "the handle" and
"the object", and both can be of different types. Languages like C++ make
this more clear, pointers are first-class-objects there. In C#, many people
think after the statement

T t = new T

with a class type T, that "t is an object of type T", which is of course
wrong.
I wouldn't be able to pass an
ExtendedPoint to a method requiring a Point, contrary to all normal
expectations.
Depends on your expectations. Why? A little exemple....
What would you expect the outcome of

void f( T t )
{
t.x = 1;
}

to be after a call to f?
- If T is of value type?
- If T is of reference type?

The outcome is very different - no compiler warning, nothing. Conclusion?
You have to simply *know* what kind of type T is....

Now back to your statement.
I wouldn't be able to pass an
ExtendedPoint to a method requiring a Point, contrary to all normal
expectations.
It is, of course possible, and inside the method, you will get a perfect
Point-object. For value types, this perfectly meets my expectations.

If your requirements are such that you need polymorphic behavior, don't use
value types. They are not appropriate for this.
I still cannot see why the behavior of value types should prevent
derivation. Derivation has other uses besides the polymorphism-enabler.

Paule

May 14 '07 #8
Paul Werkowitz <ne********@primaprogramm.dewrote:
So there'd be no polymorphism possible... sounds like a good argument
against derivation of structs to me.

Value types are not made for polymorphism, virtual functions etc.
I agree, although they work okay for implementing interfaces, provided
you don't mind the cost of boxing.

<snip parameter passing example>

I agree, you need to know whether you're dealing with a value type or a
reference type. Mutable value types are bad idea in general though, and
should be avoided, making this kind of thing less of an issue.
I wouldn't be able to pass an
ExtendedPoint to a method requiring a Point, contrary to all normal
expectations.

It is, of course possible, and inside the method, you will get a perfect
Point-object. For value types, this perfectly meets my expectations.
But it wouldn't meet mine, probably due to different backgrounds.
If your requirements are such that you need polymorphic behavior, don't use
value types. They are not appropriate for this.
No, but I suspect I'm not the only one who will expect there to be an
implicit conversion from any type to its base type.
I still cannot see why the behavior of value types should prevent
derivation. Derivation has other uses besides the polymorphism-enabler.
True, but I think you'll agree it makes derivation less useful if you
don't get polymorphism, I think which makes it a reasonable argument
against inclusion - it means the added language/framework complexity
doesn't have as much payback as might be expected on first glance.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 14 '07 #9
Am Mon, 14 May 2007 19:59:56 +0100 schrieb Jon Skeet [C# MVP]:
>Value types are not made for polymorphism, virtual functions etc.

I agree, although they work okay for implementing interfaces, provided
you don't mind the cost of boxing.
No sire, they do not. Boxing transfers the value object into something
completely different. THERE IS NO SUCH THING AS virtuality / polymorphism
etc. for value types.

I agree that you can create a reference type from a value type, and that
language designers might come to the conclusion that such conversion should
sometimes be implicit. The result of such conversion is NOT of VALUE TYPE!
<snip parameter passing example>

I agree, you need to know whether you're dealing with a value type or a
reference type. Mutable value types are bad idea in general though, and
should be avoided, making this kind of thing less of an issue.
No, I strongly disagree. Two reasons:

1.) If you have for example a Rect structure .... how can you e.g. increase
it by 1 pixel without changing the values?

2.) Original problem still remains. What do we know of the passed object
after f has executed:

void f( V v )
{
v.g();
}

if
- V is of value type?
- V is of ref type?

Seems to be the same problem to me.


>>I wouldn't be able to pass an
ExtendedPoint to a method requiring a Point, contrary to all normal
expectations.

It is, of course possible, and inside the method, you will get a perfect
Point-object. For value types, this perfectly meets my expectations.

But it wouldn't meet mine, probably due to different backgrounds.
>If your requirements are such that you need polymorphic behavior, don't use
value types. They are not appropriate for this.

No, but I suspect I'm not the only one who will expect there to be an
implicit conversion from any type to its base type.
I don't understand. You think in terms of reference-semantics. For value
types, there are no references.

I hope, after the statements

int i = 5;
int k = i;
k = 0;

you do not expect i to have the value 0......

These statements transfer values! Then it is absolutely logical that the
statement in

D d = ....
B b = d;

creates a completely new B-object and initializes it with *the values* from
d.
For value types, I do not expect that b now "points" to a D-object.
>
>I still cannot see why the behavior of value types should prevent
derivation. Derivation has other uses besides the polymorphism-enabler.

True, but I think you'll agree it makes derivation less useful if you
don't get polymorphism, I think which makes it a reasonable argument
against inclusion - it means the added language/framework complexity
doesn't have as much payback as might be expected on first glance.
Also here, I don't see this. Complexity cannot be the issue. If we say,
that we can derive at all, derivation for value types is not too complex to
comprehend at all. For beginners, the distinction between value types and
reference types is much more difficult, especially because there is no
notational difference.

Greetz
Paule
May 16 '07 #10
Paul Werkowitz <ne********@primaprogramm.dewrote:
Value types are not made for polymorphism, virtual functions etc.
I agree, although they work okay for implementing interfaces, provided
you don't mind the cost of boxing.
No sire, they do not. Boxing transfers the value object into something
completely different. THERE IS NO SUCH THING AS virtuality / polymorphism
etc. for value types.
I think it doesn't help that you can override methods. Arguably the
polymorphism is only used there when it's in its boxed
I agree that you can create a reference type from a value type, and that
language designers might come to the conclusion that such conversion should
sometimes be implicit. The result of such conversion is NOT of VALUE TYPE!
Agreed. (I think the "constrain" IL op code may make things slightly
trickier here, but that's below the surface.)
<snip parameter passing example>

I agree, you need to know whether you're dealing with a value type or a
reference type. Mutable value types are bad idea in general though, and
should be avoided, making this kind of thing less of an issue.

No, I strongly disagree. Two reasons:

1.) If you have for example a Rect structure .... how can you e.g. increase
it by 1 pixel without changing the values?
By returning a new value, just like DateTime.AddMinutes does.
2.) Original problem still remains. What do we know of the passed object
after f has executed:

void f( V v )
{
v.g();
}

if
- V is of value type?
- V is of ref type?

Seems to be the same problem to me.
Well, you should certainly still know whether you're using a reference
type or a value type, yes.
If your requirements are such that you need polymorphic behavior, don't use
value types. They are not appropriate for this.
No, but I suspect I'm not the only one who will expect there to be an
implicit conversion from any type to its base type.

I don't understand. You think in terms of reference-semantics. For value
types, there are no references.
But because of the way that .NET emphasises reference types over value
types (when making custom types, anyway) that changes the general feel
of the languages and thus expectations.
I hope, after the statements

int i = 5;
int k = i;
k = 0;

you do not expect i to have the value 0......
No, of course not. Assignment works by value for all types - it's just
it's the reference that's assigned in the reference type case.
These statements transfer values! Then it is absolutely logical that the
statement in

D d = ....
B b = d;

creates a completely new B-object and initializes it with *the values* from
d.
For value types, I do not expect that b now "points" to a D-object.
I agree it's a perfectly reasonable way of doing things, but it's not
what I'd have originally expected if this were allowed - which, of
course, it's not.
True, but I think you'll agree it makes derivation less useful if you
don't get polymorphism, I think which makes it a reasonable argument
against inclusion - it means the added language/framework complexity
doesn't have as much payback as might be expected on first glance.

Also here, I don't see this. Complexity cannot be the issue. If we say,
that we can derive at all, derivation for value types is not too complex to
comprehend at all. For beginners, the distinction between value types and
reference types is much more difficult, especially because there is no
notational difference.
There is complexity in adding the feature of deriving from structs.
There is also a benefit to it. The question is whether the benefit
outweighs the cost in complexity or not. I don't believe it does, you
presumably do. That's a perfectly reasonable matter of opinion.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 16 '07 #11
Am Wed, 16 May 2007 18:32:04 +0100 schrieb Jon Skeet [C# MVP]:
>
[snipped]
Its a matter of opinion, ok.

But to your last statement I have a question:
There is complexity in adding the feature of deriving from structs.
There is also a benefit to it. The question is whether the benefit
outweighs the cost in complexity or not. I don't believe it does, you
presumably do. That's a perfectly reasonable matter of opinion.
What complexity do you see in the feature of deriving from a struct? We
have no polymorphism here, only the possibility to override functions and
add new ones. To write non-trivial programs in C#, you need reference
types, derivation, polymorphism, ..... the whole enchilada. Such a person
should be able to grasp the concept of derivation of value types, too,
IMHO.

Greetz!
Paule
May 19 '07 #12
Paul Werkowitz <ne********@primaprogramm.dewrote:
Am Wed, 16 May 2007 18:32:04 +0100 schrieb Jon Skeet [C# MVP]:
[snipped]
Its a matter of opinion, ok.

But to your last statement I have a question:
There is complexity in adding the feature of deriving from structs.
There is also a benefit to it. The question is whether the benefit
outweighs the cost in complexity or not. I don't believe it does, you
presumably do. That's a perfectly reasonable matter of opinion.

What complexity do you see in the feature of deriving from a struct? We
have no polymorphism here, only the possibility to override functions and
add new ones.
And add new fields, presumably. However, you *do* still need to learn
what is and isn't now allowed, and what should happen in various cases.
There's more to learn, and for little benefit IMO. (I very rarely write
my own value types to start with, and can't remember ever wanting to
derive one value type from another.)
To write non-trivial programs in C#, you need reference
types, derivation, polymorphism, ..... the whole enchilada. Such a person
should be able to grasp the concept of derivation of value types, too,
IMHO.
Yes, but in the same way that someone who has learned a difficult
language (e.g. Welsh) should be able to learn a relatively simple
language (e.g. French). That doesn't mean they would instantly *know*
French having learned Welsh. They'd have more to learn. I'm all for
keeping the language as simple as possible unless there's a really good
benefit to be gained from adding complexity (e.g. the addition of
generics and nullable types). Just MHO though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 20 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cgacc20 | last post by:
I have a c struct from old code that cannot be modified and I am trying to write a wrapper C++ class around it. This class is often passed as a pointer to some c functions of a library and I...
22
by: Marcin Vorbrodt | last post by:
Taken out of C++ In a Nutshell book... Example 7-18: Using an abstract classes as interface specification. struct Runnable { virtual void run() = 0; }; struct Hashable { virtual size_t...
8
by: slurper | last post by:
hi, i'm studying some stl. i saw the pair implementation in a header-file but what i wonder is if a struct can have constructors as it seems in following snippet from the stl library. the...
3
by: HenryTseung | last post by:
Two questions, 1) Is it possible to use struct with inheritance especially in a C (not C++) program ? 2) What should I do to make some member operations in struct private ? Thanks in advance for...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
5
by: Martin Jørgensen | last post by:
Hi, Consider this code: --- beginning of code --- #include <iostream> using namespace std; class Child{ public:
46
by: clintonG | last post by:
Documentation tells me how but not when, why or where... <%= Clinton Gallagher http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx
9
by: werasm | last post by:
Hi all, What is the difference between: typedef struct { ... } MyS1; ....and...
1
by: johnsonlau | last post by:
When a class derives from a class, You can use a pointer to the parent class to delete the instance of child only when a virtual destructor declared in the parent. class Parent { virtual...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.