* Pierre Couderc:
class A
{
public :
A(){;}
};
class B : public A
{
public :
B() : A() {;}
virtual void bad() {;}
};
class C : public B
{
public :
C() : B() {;}
void bad() {;}
};
BOOL CVirtuApp::InitInstance()
{
A* many[2];
C* p = new C;
ASSERT(_CrtIsValidHeapPointer(p));
many[0] = p;
ASSERT(_CrtIsValidHeapPointer(many[0])); // FAILS here
ASSERT(many[0]== p); // OK;
...
}
When the ASSERT fails, I see that the paramter of CrtIsValidHeapPointer
is XXXX at first call and XXXX+4 at second call.
So what am I doing wrong?
Nothing as far as C++ is concerned.
When you cast up to A* (which you do in the assignment) you're not
guaranteed the same bitpattern in the pointer.
However the ASSERT you're using isn't part of C++, so it knows nothing
about C++ types and just checks the "raw" pointer value. In this case
that might be seen as a virtue. Because it would be wrong to treat the
A* pointer as a _heap_ pointer, i.e. to give it to operator 'delete',
because class A is non-polymorphic (has no virtual functions) so the
C destructor will then not be called -- see the FAQ about that.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?