problems with constructors
Fri Nov 13 16:30:00 GMT 1998
From: C. van Reeuwijk <C.vanReeuwijk@twi.tudelft.nl>
>You are right, 'fullindex' is of a base class type.
>Strictly speaking, you are right; it is not necessarily true that the
>pointer to the base class is the same as the pointer to the subclass.
>(a) I should still be able to do 'delete fullindex;', but this statement
>crashes. If I link in 'Electric fence', it tells me 'fullindex' is not a
>malloc'ed pointer. The difference in pointers is a symptom of the problem;
>the real problem is that 'delete fullindex;' crashes.
Yes, you should be able to delete fullindex, *provided* the base class
(the type fullindex is declared to point to) has a virtual destructor.
(Does it? If not, I'd expect the delete to have nasty consequences.)
I'm not familiar with Electric Fence -- from your description of the
symptoms, my first guess would be that EF only knows about C and
doesn't understand polymorphism.
>(b) If (implicit) casting a pointer to a base class changes it value,
>doesn't this violate compatibility with C, where the casting rules for
>struct pointers more-or-less fix their layout?
But C doesn't have inheritance, so there's no compatibility issue. For
POD structures ("plain old data" -- basically, C-style structs), I
believe the address of the first member is still required to be the
same as that of the struct itself. But that shouldn't be taken to
imply anything about addresses in the presence of ineritance and
>(c) Note that the value of 'fullindex' is *larger* than the value of 'this'.
>For some reason I would expect that for a superclass it would be smaller.
Why? I'd have expected it to be larger. The internal layout of the
derived class probably looks something like this:
+--------------------+ <-- Address of derived class
| Hidden data |
| (implemenation, |
| eg. vtable) |
+--------------------+ <-- Address of base class
| Base class data |
| Derived class data |
Ross Smith ................................... mailto:firstname.lastname@example.org
.............. The Internet Group, Auckland, New Zealand ..............
* * * * *
"Screw up your courage. You've screwed up everything else."
More information about the Gcc-bugs