www.digitalmars.com         C & C++   DMDScript  

D - Struct into class

reply J Anderson <REMOVEanderson badmama.com.au> writes:
What about the ability to convert a struct to a class.

struct A
{
    int X;
    void method();
}

class B : A
{

}

Would be the same as:

class B
{
    int X;
    void method();
}

You probably wouldn't be able to down cast back to the struct type, and 
it wouldn't work across libraries, but it could be useful when you want 
both a struct and class version of some object.

-Anderson
Dec 16 2003
parent reply Ilya Minkov <minkov cs.tum.edu> writes:
J Anderson wrote:
 What about the ability to convert a struct to a class.
I think this is called automatic boxing: for each struct type, on demand a corresponding class type would be automatically created. This would allow structs to be direct ancestors of Object, and thus be used in polymorfic functions. One example where this can be useful is the console inoput/output. However, if we do that, we could go back and merge struct and class *almost* as it was in C++... -eye
Dec 16 2003
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Ilya Minkov wrote:

 J Anderson wrote:

 What about the ability to convert a struct to a class.
I think this is called automatic boxing: for each struct type, on demand a corresponding class type would be automatically created. This would allow structs to be direct ancestors of Object, and thus be used in polymorfic functions. One example where this can be useful is the console inoput/output. However, if we do that, we could go back and merge struct and class *almost* as it was in C++... -eye
Interesting. Aside from copying the function interface over or indirectly referencing the struct, what do you do if you want to use a struct as a class. For instance, I want to extend Burton's (dig) vec3 struct, to add some more methods. I guess I have to use functions instead, or some tacky casting :( -Anderson
Dec 16 2003
parent reply Ilya Minkov <minkov cs.tum.edu> writes:
J Anderson wrote:

 Interesting.  Aside from copying the function interface over or 
 indirectly referencing the struct, what do you do if you want to use a 
 struct as a class.
Indirectly referencing a struct is not a very good option. This would mean that toString and toHash cannot be acessed without the source expicitly knowing the concrete type of the referenced struct. This means that switch would not work correctly, and things like extensible stream output were not implementable. A wrapping class should consist of a struct and have a VTable allowing to access struct's methods which correspond to those requiered for a Class. We could even allow a struct to inherit from an interface? Then a box could automatically implement this interface too, and thus allow for high performance stdio, among other uses.
 For instance, I want to extend Burton's (dig) vec3 struct, to add some 
 more methods.  I guess I have to use functions instead, or some tacky 
 casting :(
Current syntax allows to extend a struct. However, what you get is more or less a new struct, and even if casting to its predecessor is allowed (i don't remember exectly, IMO it should be), as a result you get a "cut down" struct of vec3 type, which would not implement the new overridden behaviour but the old one. However, as long as you only extend a type, everything would work OK. -eye -eye
Dec 17 2003
parent J Anderson <REMOVEanderson badmama.com.au> writes:
Ilya Minkov wrote:

 J Anderson wrote:

 Current syntax allows to extend a struct. However, what you get is 
 more or less a new struct, and even if casting to its predecessor is 
 allowed (i don't remember exectly, IMO it should be), as a result you 
 get a "cut down" struct of vec3 type, which would not implement the 
 new overridden behaviour but the old one. However, as long as you only 
 extend a type, everything would work OK.

 -eye
Oh, I tried: struct Vector : vec3 { } But that didn't work? I get: Declaration expected, not ':' What am I doing wrong
Dec 17 2003