What is wrong with the following code ?

R

Razvan

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
 
A

Andre Kostur

(e-mail address removed) (Razvan) wrote in
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.
 
V

Victor Bazarov

Razvan said:
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
 
X

Xiangliang Meng

Razvan said:
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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top