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

Necessity of pure virtual destructor?

P: n/a
Hi,
I wonder the necessity of constructor and destructor in a Abstract
Class? Is it really needed?

?
Wg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #1
Share this Question
Share on Google+
37 Replies


P: n/a
WittyGuy wrote:
I wonder the necessity of constructor and destructor in a Abstract
Class? Is it really needed?


If your class contains no data and produces no side effects when
constructed and/or destructed, then the default would be just fine.
If it does contain data, then you need to at least see how they are
constructed or disposed of, maybe you need to take control.

V
Jul 23 '05 #2

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:B4*****************@newsread1.mlpsca01.us.to. verio.net
WittyGuy wrote:
I wonder the necessity of constructor and destructor in a Abstract
Class? Is it really needed?


If your class contains no data and produces no side effects when
constructed and/or destructed, then the default would be just fine.
If it does contain data, then you need to at least see how they are
constructed or disposed of, maybe you need to take control.


?? As I understand it, the default destructor of the Abstract class will not
be virtual and hence derived class objects pointed to by a pointer to
Abstract will not have their destructor called when delete is called for the
Abstract pointer, e.g.,

class Abstract
{
public:
virtual void method()=0;
};

class Derived : public Abstract
{
virtual void method()
{}
~Derived()
{
// lots of stuff
std::cout << "Derived destructor\n";
}
};

int main()
{
Abstract * ptr = new Derived;
delete ptr;
}

You will observe that the destructor for Derived is never called. If,
however, you add

virtual ~Abstract(){}

to the definition of Abstract, then the Derived destructor is called.
--
John Carson

Jul 23 '05 #3

P: n/a
On 2005-06-24 09:41:51 -0400, Victor Bazarov <v.********@comAcast.net> said:
WittyGuy wrote:
I wonder the necessity of constructor and destructor in a Abstract
Class? Is it really needed?
If your class contains no data and produces no side effects when
constructed and/or destructed, then the default would be just fine.


Not true. If a class is abstract, then it, by definition, has virtual
functions, and will serve as a base class (if it is to be of any use).
In such a situation, it is usually very important to have a virtual
destructor. Whether that destructor is pure virtual or not is largely
irrelevant, but the compiler-provided one is *not* virtual, and is
therefore inadequate.
If it does contain data, then you need to at least see how they are
constructed or disposed of, maybe you need to take control.

--
Clark S. Cox, III
cl*******@gmail.com

Jul 23 '05 #4

P: n/a
John Carson wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:B4*****************@newsread1.mlpsca01.us.to. verio.net
WittyGuy wrote:
I wonder the necessity of constructor and destructor in a Abstract
Class? Is it really needed?

If your class contains no data and produces no side effects when
constructed and/or destructed, then the default would be just fine.
If it does contain data, then you need to at least see how they are
constructed or disposed of, maybe you need to take control.

?? As I understand it, the default destructor of the Abstract class will
not be virtual and hence derived class objects pointed to by a pointer
to Abstract will not have their destructor called when delete is called
for the Abstract pointer, e.g.,
[...]


Yes, if polymorphic deletion is required, then the base class destructor
has to be declared virtual.

V
Jul 23 '05 #5

P: n/a
Clark S. Cox III wrote:

Not true. If a class is abstract, then it, by definition, has virtual
functions, and will serve as a base class (if it is to be of any use).
In such a situation, it is usually very important to have a virtual
destructor. Whether that destructor is pure virtual or not is largely
irrelevant, but the compiler-provided one is *not* virtual, and is
therefore inadequate.


The compiler-generated destructor is only inadequate if you delete an
object of a derived type through a pointer to that base. Having or not
having virtual functions is irrelevant. Of course, if you want to define
an abstract class with no virtual functions, you do it by declaring the
destructor pure virtual.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #6

P: n/a
"WittyGuy" <wi**********@gmail.com> ha scritto nel messaggio
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi,
I wonder the necessity of constructor and destructor in a Abstract
Class? Is it really needed?


The constructor is needed if the Abstract class contains any data member
which needs to be initialized. Usually there shouldn't be data members in
abstract classes bu sometimes...
A destructor is *definitely* needed, otherwise you'd get undefined behavior
deleting a derived object thru a pointer to base variable.

Ciao,
Gianuca
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #7

P: n/a
That depends. There are two kinds of abstract classes in C++: classes
that don't have any state and all their methods are pure virtual
(similar to Java interfaces), and classes that have some state and/or
some of the behaviour already implemented (similar to Java abstract
classes - this is the kind of stuff you'd use, for example, to implement
the Template pattern).

In both cases, you _should_ add a virtual destructor. If you don't add a
virtual destructor, then deleting a derived class instance through a
pointer to a base class spells trouble. For more information on this,
you could check out Scott Meyers' books "Exceptional C++" and "More
Exceptional C++".

If your class is just an interface, then a constructor not only is _not_
needed, but it's useless. What would a constructor initialize, if the
class has no state?
If your class does have state information (data members) that needs to
be initialized prior to object usage, you will need to add a constructor.
Also, an abstract class might inherit from some other class that doesn't
have a default constructor, in which case you will have to initialize
the base class in an user-provided constructor.

The bottom line is: you _must_ use a constructor if your class has state
that needs to be explicitly initialized, and/or if a base class doesn't
have a default constructor. You _could_ use a constructor in several
other cases, but it's not strictly required.
And you must _always_ provide a virtual destructor if your class is
meant as a base class for some other class (which it surely is, since
it's abstract).
Hope it helps.

--
Razvan Cojocaru
KeyID: 1024D/04CA34DE

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #8

P: n/a
ben
"WittyGuy" <wi**********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi,
I wonder the necessity of constructor and destructor in a Abstract
Class? Is it really needed?
Constructor is optional.

Destructor is a must. Because you must allow user to delete the object from
the base.

Destructor must not be pure virtual. It just has to be defined. Use an empty
destructor if necessary.

ben

?
Wg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #9

P: n/a
Pete Becker wrote:
[...] if you want to define
an abstract class with no virtual functions, you do it by declaring the
destructor pure virtual.


Isn't that statement self-contradicting? If you do declare the destructor
[pure] virtual, can you say that the class has "no virtual functions"? :-)
Jul 23 '05 #10

P: n/a
Razvan Cojocaru wrote:
[...] For more information on this,
you could check out Scott Meyers' books "Exceptional C++" and "More
Exceptional C++".
That's "Effective C++" and "More Effective C++".
[...]
And you must _always_ provide a virtual destructor if your class is
meant as a base class for some other class (which it surely is, since
it's abstract).


Not if it the object of derived class is never deleted through a pointer
to the base class.

V
Jul 23 '05 #11

P: n/a
"WittyGuy" <wi**********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi,
I wonder the necessity of constructor and destructor in a Abstract
Class? Is it really needed?

Yes! Otherwise what would happen if you called delete on a pointer with
static type base, which has dynamic type derived? Only base would be
destructed properly, and derived would leak stuff. A ctor can't be virtual
btw. Oh and remember that even a pure virtual dtor must have a body, this a
little oddity but perfectly logical.

--
Regards,

Ferdi Smit
smit xs4all nl

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #12

P: n/a
Victor Bazarov wrote:
Pete Becker wrote:
[...] if you want to define an abstract class with no virtual
functions, you do it by declaring the destructor pure virtual.

Isn't that statement self-contradicting? If you do declare the destructor
[pure] virtual, can you say that the class has "no virtual functions"? :-)


Sigh. To reestablish context, the assertion that I replied to was:
If a class is abstract, then it, by definition, has virtual

functions, > and will serve as a base class (if it is to be of any use).
In such a > situation, it is usually very important to have a virtual
destructor.

My statement was that having virtual functions in itself has nothing to
do with needing a virtual destructor. You need a virtual destructor if
you're going to destroy an object of a derived type through a pointer to
that base.

I also made the observation that if a class has no virtual functions and
you want to make it abstract, you do so by declaring the destructor pure
virtual. AFTER you've done that, the class has one virtual function.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #13

P: n/a
"Pete Becker" <pe********@acm.org> wrote in message
news:y5********************@rcn.net

I also made the observation that if a class has no virtual functions
and you want to make it abstract, you do so by declaring the
destructor pure virtual. AFTER you've done that, the class has one
virtual function.


It is perhaps worth pointing out that this pure virtual function will need
an implementation in the base class.

--
John Carson

Jul 23 '05 #14

P: n/a


WittyGuy wrote:
Hi,
I wonder the necessity of constructor and destructor in a Abstract
Class? Is it really needed?


Ctor: no - the compiler will create one for you.
Dtor: should be protected, virtual or both. The default is neither.

It is possible to write a correct program with a public non-virtual
dtor. This is simply because removing an access check from a correct
program levaes it correct. Removing 'protected' from a dtor is just
one case of this rule. However, without 'protected' you're not
protected against the delete-via-non-virtual-dtor bug.

HTH,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #15

P: n/a
On 2005-06-24 11:35:29 -0400, Pete Becker <pe********@acm.org> said:
Clark S. Cox III wrote:

Not true. If a class is abstract, then it, by definition, has virtual
functions, and will serve as a base class (if it is to be of any use).
In such a situation, it is usually very important to have a virtual
destructor. Whether that destructor is pure virtual or not is largely
irrelevant, but the compiler-provided one is *not* virtual, and is
therefore inadequate.

The compiler-generated destructor is only inadequate if you delete an
object of a derived type through a pointer to that base.


....and such a situation is exceedingly likely for most uses of abstract
classes. Hence my saying "usually", and not "always".
Having or not having virtual functions is irrelevant.
Every abstract class has virtual function by definition.
Of course, if you want to define an abstract class with no virtual
functions, you do it by declaring the destructor pure virtual.


So, to define an abstract class with no virtual functions ... you add a
virtual function?

--
Clark S. Cox, III
cl*******@gmail.com

Jul 23 '05 #16

P: n/a
WittyGuy wrote:
Hi,
I wonder the necessity of constructor and destructor in a Abstract
Class? Is it really needed?


Absolutely. You can't create a class without a constructor or without
a destructor. (If you don't specify one, the compiler will do it for
you.)

I suspect that by "Abstract Class" you mean a class with just pure
virtual functions and no data members. Note that C++ allows base
classes with data members and real member functions.

I think you meant to ask about user specified constructors and
destructors. If so, there is no overriding need for a user specified
constructor (though of course, a particular class might benefit from
one - particularly if the class has data members which need
initializing).

In the case of a destructor I would be very surprised if you didn't
want to declare one. The problem is that the compiler generated
destructor will be public and non-virtual (assuming the class in
question has no bases with virtual destructors). This means there is
nothing to stop someone doing:
delete pAbstractClass;
where the actual object pointed at is of some derived type. If the
destructor is not virtual this is "undefined behaviour", which
basically means "bad things may happen".

There are two ways (I know of) to avoid the bad things. One is to
declare a virtual destructor (which avoids the bad things), the other
is to make the destructor protected (which makes the statement a
compilation error). Both of these methods require the destructor to be
declared.

There is less point making the destructor PURE virtual though (provided
at least one other function is pure virtual). A pure virtual function
a) has to be overriden by a derived class (but destructors have to be
overriden anyway); b) prevent the base class from being instantiated as
a specific object (but another pure virtual function will do that
anyway).

The other thing about pure virtual functions is that they don't need to
be implemented (although they can be). The exception is the destructor
- even if the destructor is pure virtual, it has to be implemented
(somewhere).

that means
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #17

P: n/a
Ferdi Smit wrote:

Yes! Otherwise what would happen if you called delete on a pointer with
static type base, which has dynamic type derived? Only base would be
destructed properly, and derived would leak stuff.


That's one possibility, but formally, the behavior is undefined.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #18

P: n/a
Clark S. Cox III wrote:

So, to define an abstract class with no virtual functions ... you add a
virtual function?


Yes. If you have a class with no virtual functions and you want it to be
an abstract class you make its destructor pure virtual.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #19

P: n/a

"ben" <be******@hotmail.com> wrote in message
news:42***********************@news.optusnet.com.a u...
"WittyGuy" <wi**********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi,
I wonder the necessity of constructor and destructor in a Abstract
Class? Is it really needed?


Constructor is optional.

Destructor is a must. Because you must allow user to delete the object
from
the base.


I take it you mean "...to delete a derived-class object via a pointer to a
base-class object"?

That's what a _virtual_ destructor gives you the aility to do. It's not a
requirement if you don't need to do that, and it needs to be declared
virtual in order to accomplish that.

-Howard


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #20

P: n/a


Razvan Cojocaru wrote:
That depends. There are two kinds of abstract classes in C++: classes
that don't have any state and all their methods are pure virtual
(similar to Java interfaces), and classes that have some state and/or
some of the behaviour already implemented (similar to Java abstract
classes - this is the kind of stuff you'd use, for example, to implement
the Template pattern).

In both cases, you _should_ add a virtual destructor. If you don't add a
virtual destructor, then deleting a derived class instance through a
pointer to a base class spells trouble. For more information on this,
you could check out Scott Meyers' books "Exceptional C++" and "More
Exceptional C++".

Not that it is that big of a deal, but Herb Sutter wrote "Exceptional
C++" and "More Exceptional C++" and not Scott Meyers. Meyers wrote
"Effective C++", "More Effective C++", and "Effective STL". The series
names are easy to confuse:).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #21

P: n/a
John Carson wrote:
"Pete Becker" <pe********@acm.org> wrote in message
news:y5********************@rcn.net

I also made the observation that if a class has no virtual functions
and you want to make it abstract, you do so by declaring the
destructor pure virtual. AFTER you've done that, the class has one
virtual function.

It is perhaps worth pointing out that this pure virtual function will
need an implementation in the base class.


Which, of course, can simply do nothing, if no action is called for:

ABC::~ABC() {}

--
Mike Smith
Jul 23 '05 #22

P: n/a
Martin Bonner wrote:
Absolutely. You can't create a class without a constructor or without
a destructor. (If you don't specify one, the compiler will do it for
you.)


Not to be pedantic, but the compiler can tell from the class definition
whether a a destructor is needed or not:

struct Foo
{
} ;

int main ()
{
Foo foo;
return 0;
}

// Assembly language output:

struct Foo
{
} ;

int main ()
{
00411F40 push ebp
00411F41 mov ebp,esp
00411F43 sub esp,0CCh
00411F49 push ebx
00411F4A push esi
00411F4B push edi
00411F4C lea edi,[ebp-0CCh]
00411F52 mov ecx,33h
00411F57 mov eax,0CCCCCCCCh
00411F5C rep stos dword ptr [edi]
Foo foo;
return 0;
00411F5E xor eax,eax
}
00411F60 push edx
00411F61 mov ecx,ebp
00411F63 push eax
00411F64 lea edx,ds:[411F78h]
00411F6A call @ILT+605(@_RTC_CheckStackVars@8) (411262h)
00411F6F pop eax
00411F70 pop edx
00411F71 pop edi
00411F72 pop esi
00411F73 pop ebx
00411F74 mov esp,ebp
00411F76 pop ebp
00411F77 ret

-Chaud Lapin-
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #23

P: n/a
> > Destructor is a must. Because you must allow user to delete the object
from
the base.


I take it you mean "...to delete a derived-class object via a pointer to a
base-class object"?

That's what a _virtual_ destructor gives you the aility to do. It's not a
requirement if you don't need to do that, and it needs to be declared
virtual in order to accomplish that.


In fact, a virtual destructor provides a correct interface for 'delete
the_plain_pointer_to_base_class' operation.
If you don't expect to do this, you may leave the destructor to be
nonvirtual and even implicitly defined.

1) Scalarly deleted objects (allocated on stack, or static, or members
of other objects).

void foo(Base& obj); // will call some obj->method() that should be
virtual
int main() { Derived obj; foo(obj); }

2) Objects have a custom deletion function. Most known example is
IUnknown::Release that implementation deals with the final class, not
with a base.

A modification of this example: a smart pointer that knows the deleter.

template<class B, class D>
void delete_derived(B* b) { delete static_cast<D*>(b); }

shared_ptr<Base> p (new Derived(), delete_derived<Base,Derived>)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #24

P: n/a
In article <11**********************@g47g2000cwa.googlegroups .com>,
Le Chaud Lapin <un*****************@yahoo.com> wrote:
Martin Bonner wrote:
Absolutely. You can't create a class without a constructor or without
a destructor. (If you don't specify one, the compiler will do it for
you.)


Not to be pedantic, but the compiler can tell from the class definition
whether a a destructor is needed or not:

// Assembly language output: ..ASM CODE SNIPPED...


True, but to be even more pedantic :), that's an optimization
on the generated code and does not negate other rules, etc.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #25

P: n/a
Greg Comeau wrote:
In article
<11**********************@g47g2000cwa.googlegroups .com>, Le
Chaud Lapin <un*****************@yahoo.com> wrote:
Martin Bonner wrote:
Absolutely. You can't create a class without a constructor
or without a destructor. (If you don't specify one, the
compiler will do it for you.)
Not to be pedantic, but the compiler can tell from the class
definition whether a a destructor is needed or not: // Assembly language output: ..ASM CODE SNIPPED...

True, but to be even more pedantic :), that's an optimization
on the generated code and does not negate other rules, etc.


According to the language rules, a class always has a
constructor and a destructor (which is what Greg is saying).
The standard does distinguish between trivial and non-trivial
constructors and destructors, however -- you cannot put a class
with a non-trivial constructor or destructor in a union, for
example. Since the compiler must make this distinction, it
would be perfectly surprising if it actually generated any code
for a trivial constructor or destructor. For that matter: given
that the compiler generated defaults are inline, what code could
it possibly generate. When you inline an empty function,
there's not much left.

Of course, there's no concept of "the compiler knowing whether a
destructor is needed or not". I'm not even sure what that
really means.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #26

P: n/a
meadori wrote:
Razvan Cojocaru wrote:
That depends. There are two kinds of abstract classes in
C++: classes that don't have any state and all their methods
are pure virtual (similar to Java interfaces), and classes
that have some state and/or some of the behaviour already
implemented (similar to Java abstract classes - this is the
kind of stuff you'd use, for example, to implement the
Template pattern). In both cases, you _should_ add a virtual destructor. If you
don't add a virtual destructor, then deleting a derived
class instance through a pointer to a base class spells
trouble. For more information on this, you could check out
Scott Meyers' books "Exceptional C++" and "More Exceptional
C++".
Not that it is that big of a deal, but Herb Sutter wrote
"Exceptional C++" and "More Exceptional C++" and not Scott
Meyers. Meyers wrote "Effective C++", "More Effective C++",
and "Effective STL". The series names are easy to confuse:).


The obvious solution is to read both:-). For the type of
questions posed by the original poster, "Effective C++" is the
essential book; it treats the simplest, lowest level questions,
such as when do you need a constructor or a destructor.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #27

P: n/a
> That's "Effective C++" and "More Effective C++".

Yes, you're right. Somebody else also pointed that out. Easy to confuse
names and a tight schedule tends to do that to people.
[...]
And you must _always_ provide a virtual destructor if your class is
meant as a base class for some other class (which it surely is, since
it's abstract).

Not if it the object of derived class is never deleted through a pointer
to the base class.


That's like saying that you only need to keep sharp objects from childen
if they intend to stick them in their eyeballs.

--
Razvan Cojocaru
KeyID: 1024D/04CA34DE
Jul 23 '05 #28

P: n/a
Razvan Cojocaru wrote:

That's like saying that you only need to keep sharp objects from childen
if they intend to stick them in their eyeballs.


You must hang out with a bunch of rather bad programmers.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #29

P: n/a
> Not that it is that big of a deal, but Herb Sutter wrote "Exceptional
C++" and "More Exceptional C++" and not Scott Meyers. Meyers wrote
"Effective C++", "More Effective C++", and "Effective STL". The series
names are easy to confuse:).


You're right, of course. I had a lot on my mind and it slipped :-).

--
Razvan Cojocaru
KeyID: 1024D/04CA34DE

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #30

P: n/a
ka***@gabi-soft.fr wrote:
Of course, there's no concept of "the compiler knowing whether a
destructor is needed or not". I'm not even sure what that
really means.


Ooops, I meant constructor.

-Chaud Lapin-
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #31

P: n/a
Le Chaud Lapin wrote:
ka***@gabi-soft.fr wrote:
Of course, there's no concept of "the compiler knowing
whether a destructor is needed or not". I'm not even sure
what that really means.
Ooops, I meant constructor.


But I still don't understand what it means to say "the compiler
knows whether it is needed or not". According to the language
definition, all classes have a constructor, which is used to
initialize the object. Some constructors are trivial, others
not; the language definition says which, and since what you can
do with an object partially depends on whether the constructor
is trivial or not, the compiler must be able to determine this.

All of which, of course, is irrelevant with regards to generated
code. Typically, if the constructor is inline, and not too
complicated, the compiler will inline it. (Trivial constructors
are always inline.) And if the constructor causes no code to be
generated (regardless of whether it is "trivial" in the language
sense of the word), the compiler will generate no code for it if
it inlined. That doesn't mean that the constructor or
destructor "aren't needed" -- I have a number of cases in my
code where constructors or destructors aren't trivial, and are
needed, but where the compiler doesn't generate any code for
them.

(I wonder if any compilers actually completely eliminate the
virtual destructor of an abstract class in which the destructor
doesn't do anything.)

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #32

P: n/a
If destructor of either ABC or just base class needs to be virtual, why
don't we have virtual constructor?

--Wg-
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #33

P: n/a


WittyGuy wrote:
If destructor of either ABC or just base class needs to be virtual, why
don't we have virtual constructor?


You (and the compiler) always know the precise type of the object you
are creating at the point of creation, but if you delete an object
through a pointer to a base class you (and the compiler) do not
(necessarily) know the precise type of the object being destroyed.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #34

P: n/a
In article <11**********************@g44g2000cwa.googlegroups .com>,
WittyGuy <wi**********@gmail.com> writes
If destructor of either ABC or just base class needs to be virtual, why
don't we have virtual constructor?


Because we construct things upwards but the compiler 'knows' where we
are aiming. We destroy things downwards but the compiler, given just a
pointer, does not know where the top is. Unless warned off it will use
the static type of the pointer rather than the dynamic type we require.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #35

P: n/a


WittyGuy wrote:
If destructor of either ABC or just base class needs to be virtual, why
don't we have virtual constructor?


How would you use it?

Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #36

P: n/a
WittyGuy wrote:
If destructor of either ABC or just base class needs to be
virtual, why don't we have virtual constructor?


Because the compiler needs an instance of an object in order to
resolve the virtuality. When you call a constructor, you don't
yet have an instance.

For that matter, it's not just a problem for the compiler. How
do I, the programmer, specify what type is to be constructed?

There is one exception where it might make sense: the copy
constructor. But even that would go against the rest of the
language, since the virtuality would be based on the parameter
type, and not the object on which the function is called. So we
resort to the usual convention of a clone function, e.g.:

Base* Base::clone()
{
Base* result = doClone() ;
assert( typeid( *result ) == typeid( *this ) ) ;
return result ;
}

(doClone(), of course, is a virtual function which must be
overridden in every derived class.)

All of the other cases where I've wanted some dynamic selection
of the type of object being constructed, the selection depended
on some application specific information. This is easily
handled by a map to pointers to a factory (base) class or to a
prototype, but it's hard to see what you could standardize in
the language here, since the information determining the type is
application specific.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #37

P: n/a
Pete Becker wrote:
Razvan Cojocaru wrote:

That's like saying that you only need to keep sharp objects from
childen if they intend to stick them in their eyeballs.


You must hang out with a bunch of rather bad programmers.


Children that never grow up? :-p

Ben
--
A7N8X FAQ: www.ben.pope.name/a7n8x_faq.html
Questions by email will likely be ignored, please use the newsgroups.
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #38

This discussion thread is closed

Replies have been disabled for this discussion.