using destructor

T

Tony Johansson

Hello Experts!

I have one easy question for you.

If I have an abstract class is it any point to define a destructor with some
code within the body because an abstrct class can not be instansiated.

If you dont't agree with me please give some example.

Many thanks.

//Tony
 
S

Sharad Kala

Tony Johansson said:
Hello Experts!

I have one easy question for you.

If I have an abstract class is it any point to define a destructor with some
code within the body because an abstrct class can not be instansiated.

Yes, it makes sense.

struct A {
virtual ~A() = 0;
};

struct B : A {

};

int main()
{
B b;
}

Compile and LINK the above program. It makes sense to define a constructor
for the base class which is abstract.

Sharad
 
S

Srini

I have one easy question for you.
If I have an abstract class is it any point to define a destructor with some
code within the body because an abstrct class can not be instansiated.
If you dont't agree with me please give some example.
Many thanks.

A class being abstract does not mean that it must not have attributes
of its own. An abstract class is intended to be inherited by other
class/classes. It might have some common attributes and methods in the
intended heirarchy of classes. It is the responsibility of the abstract
base class to properly initialize(ctor) and clean-up(dtor) its
attribute members. During the destruction of a derived class object,
the base class(es)' destructor would always be called irrespective of
whether the base class(es) is(are) abstract or not. Hope it answered
your query.

Regards,
Srini
 
R

ravinderthakur

this makes sense but only if u are allocating some resources in the
constructor/functions
that are to be deleted only when the class object is destroyed.

eg:


class abstract{
public:
abstract(unsigned int bytes){
data = new char[bytes];
}

......
//some more functions
.....

~abstract(){
delete[] data;
}

private:
char* data;
};


In general if u are not using the default constructor, u should not be
using the default destructor ( but not always).


thanks,
rt
 
R

Richard Herring

In message <[email protected]>,
(e-mail address removed) writes

Please learn how to quote context.

[does it make sense to define dtor for an abstract class?]
this makes sense but only if u are allocating some resources in the
constructor/functions
that are to be deleted only when the class object is destroyed.

It makes perfect sense even if you are not. Abstract base classes are
intended to be derived from, and accessed via pointers or references to
the base type. How else do you ensure that the correct virtual
destructor gets called?

IOW, the key thing here is not that the destructor has a body, but that
it's virtual.

[snip]
In general if u are not using the default constructor, u should not be
using the default destructor ( but not always).

Rarely. If you want a generalization, "in general you should be building
your classes from objects which clear up after themselves." Then only
low-level constructs which wrap raw pointers or resource handles are
likely to need anything in their destructor bodies.
 
F

Frank Chang

Sharad, As Tony Johansson points out the pure virtual destructor must
be defined. Otherwise , if the definition were not supplied you could
still derive classes from A but they could never be instantiated.
 
R

ravinderthakur

for good discussion on this issue please see the item 14 of Effective
C++
 
R

Ron Natalie

Srini wrote:
During the destruction of a derived class object,
the base class(es)' destructor would always be called irrespective of
whether the base class(es) is(are) abstract or not. Hope it answered
your query.
Yes, but there is also the rule that if you delete a derived object
through a pointer to the base class, the base class destructor MUST
be declared virtual.
 
F

Frank Chang

Ravindertha writes:
"this makes sense but only if u are allocating some resources in the
constructor/functions
that are to be deleted only when the class object is destroyed.
eg:
class abstract{
public:
abstract(unsigned int bytes){
data = new char[bytes];
}
~abstract(){
delete[] data;
}
private:
char* data;
}; "



No, I believe you are incorrect. The virtual abstract base class
constructor must always be defined, irregardless of whether there is
pointer member variable. Please read the ARM by Bjarne Strousoup. Thank
you
 

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,204
Messages
2,571,065
Members
47,672
Latest member
svaraho

Latest Threads

Top