Problem with declaration of virtual function

P

Pierre Couderc

I have a problem with the declaration of virtual function. What do I do
wrong? Why the compiler (MS VC++ 6) says nothing?

It is very simple (see my comment below):

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?

Thank you in advance
Pierre Couderc
 
A

Alf P. Steinbach

* 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.
 
P

Pierre Couderc

Alf said:
* 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.
In fact, my problem was in the delete :
I did :
delete many[0];
and then I go the ASSERT.


I suppose the error is there, please correct me : I cannot call a delete
of a A object, expecting that it will delete correctly the C object,
isn't it?

Thank you, it much more clear now.

Pierre Couderc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,945
Members
47,495
Latest member
Jack William

Latest Threads

Top