On Monday, 30 April 2012 at 05:07:04 UTC, Ali Çehreli wrote:
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.
I was thinking the 'string' which was a fat pointer, but likely
I misread it and assumed the size was referring to the object's
contents. 4bytes would be a single pointer, so my mistake on that
part.
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.
But unlike in C++, it only incorporates it if you add 'virtual',
so the up and down casting can revert to it's previous version.
I've read about it more than used it so..
//c++ - not tested..
class Base {
void func() { stdout << "base!";}
}
bass Derived : Base {
virtual void func() {stdout << "derived (virtual)";}
}
void main(){
//use pointers? I can't remember...
Derived d = new Derived;
base b = (Base) d;
b.func(); //Logically should call the derived one, but since
there's no virtual table, we get the base's one instead!!
}
Inside the derived class it's a virtual call, change it back to
it's base and it loses that extra information. You may be able to
up/down cast, but it's a problem in C++. One more reason I don't
use C++ if I don't have to.
Correction: It is actually an upcast, which does not require
the explicit cast anyway.
I am getting up and down backwards. My bad..
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.