struct to/from void, object to/from void

Mike Wey mike-wey at example.com
Mon Apr 30 02:45:33 PDT 2012


On 04/30/2012 03:01 AM, Era Scarecrow wrote:
> On Monday, 30 April 2012 at 00:28:15 UTC, Jason King wrote:
>> myobject.sizeof returns 4 (in 32 bit DMD) for every object I've
>> tested, so I'm inclined to suspect its a bog-standard pointer,
>> just what I'm looking to save and retrieve.
>> Anybody else want to chime in?
>
> I'd say that's right and wrong. Someone correct me if I'm wrong.
>
> The 4(bytes) is likely the fat pointer of the string. It still has to
> contain information saying it's this type of object, and inheritance if
> it was of another type. I believe it will be something like you have a
> 16 byte header specifying the object(s) information, and then the object
> data (4 bytes in this case). This is where it differs from C++ and acts
> more like java.
>
> A heavy part of why this is the case is not only for casting up and
> down, but management of virtual functions (overloaded). Otherwise you
> get into C++'s old mess of needing to explicitly saying virtual for
> every function that 'MIGHT' be overloaded in the future.
>
> I haven't tested this, but you should get the idea.

I have ;)

> class A {}
> class BA : A{}
>
> //returns a BA object from it's A superclass
> A test() {
> return cast(A) new BA(); //cast down
> }
>
> void test2() {
> A a = test();
> BA ba = cast(BA) a; //fails if we have no information, we only see class A.

Works because a was originally created as an BA we can cast is back to 
an BA.

> assert(ba !is null, "Failed! ba not inherited from A?");
> }

-- 
Mike Wey


More information about the Digitalmars-d-learn mailing list