
address of base class and derived class different
Quote:
>when a base class does not have a virtual member, but the
>derived class does, the MSVC++ compiler 'slices' the vptr
>table from the object, when referencing the derived object
>through a pointer to the base class object.
>Linux GNU does not do this: there the address/pointer is
>the same in both cases.
>What does the C++ standard say about this?
This is all unspecified.
Quote:
>And if it is
>implementation dependent, how do you find out what address
>to provide to delete()?
The only pointers you can delete which are not the same in value and
type as what new returned to you are pointers to base class subobjects
(of those new'd objects) which have virtual destructors. Note that
this freedom doesn't apply to the array form of new. Violations of
these rules result in undefined behavior.
Quote:
>Sample code:
>#include <iostream>
>using namespace std;
>class B
>{
>public:
> int i;
>};
>class D: public B
>{
>public:
> virtual ~D(){}
>};
>void doSomething( B * pb )
>{
> cout << hex << pb << endl;
>}
>void main()
>{
> D * pd = new D;
> cout << hex << pd << endl;
> doSomething( pd );
>}
>Results in my system in:
>00301E60
>00301E64
This is fine. Per what I described above, it would be undefined for
doSomething() to delete pb in your example, because B does not have a
virtual dtor, and pb points to the B part of a D.
--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows