473,549 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is wrong with the following code ?

Hi!

What is wrong with the following code ?
// Test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
class CTest1
{
public:
CTest1(){}
virtual ~CTest1() = 0;
};

class CTest2: public CTest1
{
public:
CTest2(){}
virtual ~CTest2(){}
};

int main(int argc, char* argv[])
{
printf("Hello World!\n");

CTest2 test;
return 0;
}

Because the desctructor for the class CTest1 is pure virtual I cannot
instantiate variables of type CTest2 ?!! Since the destructor is
not inherited the only option is to provide a body for the pure
virtual destrcutor. (I forgot to mention that I get a linker error on
VC++6.0 and VC++7.0)
Is this normal behaviour ? I mean why I cannot instantiate CTest2 ?
Because the destructor is not inherited the fact that the base
destructor is pure virtual should be irrelevant.
Regards,
Razvan
Jul 22 '05 #1
3 1805
mi*****@mailcit y.com (Razvan) wrote in
news:15******** *************** ***@posting.goo gle.com:
Hi!

What is wrong with the following code ?
// Test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
class CTest1
{
public:
CTest1(){}
virtual ~CTest1() = 0;
};

class CTest2: public CTest1
{
public:
CTest2(){}
virtual ~CTest2(){}
};

int main(int argc, char* argv[])
{
printf("Hello World!\n");

CTest2 test;
return 0;
}

Because the desctructor for the class CTest1 is pure virtual I
cannot
instantiate variables of type CTest2 ?!! Since the destructor is
not inherited the only option is to provide a body for the pure
virtual destrcutor. (I forgot to mention that I get a linker error on
VC++6.0 and VC++7.0)
Is this normal behaviour ? I mean why I cannot instantiate CTest2
?
Because the destructor is not inherited the fact that the base
destructor is pure virtual should be irrelevant.


This is normal. Recall that when CTest2 goes out of scope, the
destructor for CTest2 must be executed, and so must the destructor for
the base class, CTest1! Note that destructors (and constructors for that
matter) aren't inherited, but they do get invoked through other
mechanisms.
Jul 22 '05 #2
Razvan wrote:

What is wrong with the following code ?
Several things.


// Test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
This is a non-standard header. Perhaps it's part of your project,
but you forgot to post its contents.


class CTest1
{
public:
CTest1(){}
virtual ~CTest1() = 0;
You made the destructor pure and haven't defined it anywhere.
};

class CTest2: public CTest1
{
public:
CTest2(){}
virtual ~CTest2(){}
};

int main(int argc, char* argv[])
{
printf("Hello World!\n");

CTest2 test;
return 0;
}

Because the desctructor for the class CTest1 is pure virtual I cannot
instantiate variables of type CTest2 ?!!
No, but it will attempt to call it and the linker should report
an "unresolved external" type of error message.
Since the destructor is
not inherited the only option is to provide a body for the pure
virtual destrcutor.
Correct.
(I forgot to mention that I get a linker error on
VC++6.0 and VC++7.0)
As you are supposed to.
Is this normal behaviour ?
Absolutely.
I mean why I cannot instantiate CTest2 ?
You can. Who said you can't? You just can't dispose of it without
defining the destructor for CTest1.

Your problem is that the destructor for CTest2 makes a chained call
to the destructor of the base class. For that call, the linker has
to resolve the symbol [unmangled] CTest1::~CTest1 (), which is used
by ~CTest2 implicitly and _statically_ (i.e. not polymorphically ).
Because the destructor is not inherited the fact that the base
destructor is pure virtual should be irrelevant.


Destructor is not inherited, but it is still used.

Victor
Jul 22 '05 #3
"Razvan" <mi*****@mailci ty.com> wrote in message
news:15******** *************** ***@posting.goo gle.com...
Hi!

What is wrong with the following code ?
// Test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
class CTest1
{
public:
CTest1(){}
virtual ~CTest1() = 0;
};

class CTest2: public CTest1
{
public:
CTest2(){}
virtual ~CTest2(){}
};

int main(int argc, char* argv[])
{
printf("Hello World!\n");

CTest2 test;
return 0;
}

Because the desctructor for the class CTest1 is pure virtual I cannot
instantiate variables of type CTest2 ?!! Since the destructor is
not inherited the only option is to provide a body for the pure
virtual destrcutor. (I forgot to mention that I get a linker error on
VC++6.0 and VC++7.0)
Is this normal behaviour ? I mean why I cannot instantiate CTest2 ?
Because the destructor is not inherited the fact that the base
destructor is pure virtual should be irrelevant.
Regards,
Razvan


You should define the pure destructor of the base class even it is a pure
one, because it will be invoked when the destructor of the derived class is
invoked.

The constructor and destructor are special function members. Unlike other
function members, they are called in a hierarchy way.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
14583
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
10
3386
by: Greener | last post by:
Hi, I need help badly. Can you do client-side programming instead of server-side to capture the Browser type info? If this is the case, what's wrong with the following? <script language="JavaScript"> function doWord(file) { if (navigator.userAgent.indexOf("MSIE")!=-1)
72
5759
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to...
17
2618
by: Paul | last post by:
HI! I get an error with this code. <SCRIPT language="JavaScript"> If (ifp==""){ ifp="default.htm"} //--></SCRIPT> Basicly I want my iframe to have a default page if the user enters in directly. so I need a way doing this. so I check to see if the ifp value is null and if so then assign it a value. is this correct?
56
4291
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on C. wat type of questions should be expected? Which part of C language do the staff give more concern? The interviewers have just mentioned that .....
13
5015
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
3
1524
by: Terry Olsen | last post by:
I've got 2 different web pages, both updating the same SQL database. One is for the Technician and one is for the Manager. The technician's update page works fine but the Manager's update page doesn't. They are both the same..as far as I can tell... --------------------- This code works fine, updating the SQL database with the edited...
98
4522
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
4
6675
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
7
4255
by: mosfet | last post by:
HI, when trying to compile an embedded version of STL called ustl on win32 platform I get the following error : /// Returns the minimum of \p a and \p b template <typename T1, typename T2> inline const T1 min (const T1& a, const T2& b) { return (a < b ? a : b);
0
7532
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7461
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7730
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7971
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5101
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3509
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.