By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,503 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,503 IT Pros & Developers. It's quick & easy.

Problem with declaration of virtual function

P: n/a

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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* 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?
Jul 22 '05 #2

P: n/a
Alf P. Steinbach wrote:
* 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
Jul 22 '05 #3

P: n/a
* Pierre Couderc:

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?


Simply make the A destructor virtual to make that work; see the FAQ
at (Google, click, click, click, back, click) <url:
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.5>.

--
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?
Jul 22 '05 #4

P: n/a
Alf P. Steinbach wrote:
* Pierre Couderc:
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?

Simply make the A destructor virtual to make that work; see the FAQ
at (Google, click, click, click, back, click) <url:
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.5>.

fabs

Very instructive.

Thank you.

Pierre Couderc
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.