473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem with declaration of virtual function


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
4 1225
* 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
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
* 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
2
by: gg | last post by:
I am facing some problems with following program. I am using aCC version 03.27 on HP-UX11. The command line I use to compile is - aCC -AA -I. TestTempMethods.C Can anybody pls suggest how to...
2
by: Hans Stoessel | last post by:
Hi In the Adobe Indesign development I have e.g. the following declaration in the header file of an interface: virtual int test(int i) = 0; Whats the "= 0" at the end of the declaration? I...
8
by: Heiner | last post by:
Hi! The following snippet class B; class A { public:
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
9
by: silversurfer2025 | last post by:
Hello everyone, I am currently having problems with a C++ abstract class. I have a class FrameWork.h which defines some methods (of which some are abstract, i.e. virtual void method() = 0). In...
5
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to...
2
by: syang8 | last post by:
Dear all, I am trying to design classes with stream support. Basically, I want the operator << work for the base class and all the derived classes. If the base class is a template class, and...
8
by: Stefano Sabatini | last post by:
Hi all, I'm encountering this while trying to implement a factory singleton method to generate objects. The singleton has a static map which binds a static creation function defined in each...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.