| giancarlogiuffra (2) | ||||
|
Hello everyone, I have the following piece of code:
I was expecting to obtain the exact same address of memory 3 times, but this is not the case. The output is the following:
Any idea why? Thanks in advance for the explanation. | ||||
|
Last edited on
|
||||
| pogrady (411) | |
| i'm not sure how you even go that to happen. class A, class B, and class C are totally different, and the compiler should have flagged that the pointers are you are trying to assign are of different base classes. | |
|
|
|
| giancarlogiuffra (2) | ||
Regarding pogrady post:
I have created 2 pointers of 2 different base classes and assigned to each one of them the address of an instance of the class that is derived from both of these base classes. No problem with that. Just try to compile it. If you know the answer to the question post it, if not, then don't post anything, thanks. | ||
|
|
||
| tntxtnt (61) | ||||
|
I don't know for sure, but if you consider class as struct: class A's objects will have size of 4 bytes (only an integer) B's - 8 bytes (only a double) C's: derive from A and B, so it will have at least 4+8 or 8+4 = 12+ bytes depends on your declaration the structure of a C's object will be
so a class A pointer point at C will point at either 0 (case 1) or 8 (case 2) and class B pointer point at C will point at either 4(case 1) or 0 (case 2) and for calling class A methods from *b1, the compiler will use 4 bytes of d after b1, calling class B methods from *b2, the compiler will use 8 bytes of d after b2, calling an override method of class C will use all bytes of d. I guess your case should be case 1, because you declare public A, public B, so A first B second, 4+8, pointer b1 and b2 should be different by 4, not 8 :/ My output:
b1 and b2 is different by 4 | ||||
|
Last edited on
|
||||