struct to/from void, object to/from void
Ali Çehreli
acehreli at yahoo.com
Sun Apr 29 22:07:03 PDT 2012
On 04/29/2012 06:01 PM, 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.
You mean "object"? A class variable is just a handle to the class
object. Class variables are implemented as pointers, so yes, they will
be the size of a pointer. Since I suspect that the pointers are 4 bytes
on the OP's 32-bit system, I don't see any fatness there. It is just a
plain pointer.
> 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.
The object itself has some data that makes it behave like its actual
type but I wouldn't be surprised if it contained a virtual function
table pointer just like in C++. But note that the object, not the
variable is fat in that sense.
> 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.
> class A {}
> class BA : A{}
>
> //returns a BA object from it's A superclass
> A test() {
> return cast(A) new BA(); //cast down
Correction: It is actually an upcast, which does not require the
explicit cast anyway.
> }
>
> void test2() {
> A a = test();
> BA ba = cast(BA) a; //fails if we have no information, we only see
class A.
And that's a down cast.
> assert(ba !is null, "Failed! ba not inherited from A?");
> }
I find the following message more descriptive:
assert(ba !is null, "a is not actually a BA?");
To be more pedantic, 'a' is neither an A nor a BA. It is a variable that
provides access to an object through the A interface.
Ali
More information about the Digitalmars-d-learn
mailing list