On May 6, 8:28 am, gwowen<gwo...@gmail.com> wrote:
On May 5, 7:37 pm, ?? Tiib<oot...@hot.ee> wrote:
Hmm ... really? Where does standard say that POD base sub-object when
used with single inheritance should be located at very start of object
of derived class?
The standard doesn't. Every single implementation that is or ever
will be in existence does.
Uhh. I never dare to be so absolute about C++ compilers. Here i can
even provide evidence of opposite with a compiler manufactured by
CString creators themselves.
<code>
// WARNING: this is meant as example
// of really awful coding practices
#include<iostream>
#include<cstdio>
struct Pod { int p; };
class DerivedFromPod
: public Pod // single derived
{
public:
DerivedFromPod() { p=42; d=0; };
virtual ~DerivedFromPod() {};
private:
int d;
};
int main()
{
DerivedFromPod* der = new DerivedFromPod();
Pod* pod = der; // implicit cast here
std::cout<< "der is at: "<< der
<< " pod is at: "<< pod<< std::endl;
printf( "ints from der %d, %d \n", *der );
printf( "ints from pod %d, %d \n", *pod );
delete der;
}
</code>
Compiling it for Win32 MS compiler Visual C++ 0.9 (bundled in VS
"CString" 2008 Professional)
Running it produces something like:
der is at: 00356940 pod is at: 00356944
ints from der 4290588, 42
ints from pod 42, -242263521
So there we are with your "Every single implementation that is or ever
will be in existence does".
hierarchy, but use static_cast which adjusts pointer values appropriately.
one would not introduce virtual methods in derived class.
layout. I'm not sure but I think that for C++0x the compiler would have to stop
such practice, if it ever did. I.e., considerations of layout are not inherently
inappropriate, but one needs to be very careful (like, no virtuals).