File: destructor.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (32 lines) | stat: -rw-r--r-- 1,840 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
In the previous section policy classes were used as base classes of template
classes. This resulted in the interesting observation that a policy class
may serve as a em(base class)
        hi(class template: as base class)
    of a derived class.
    As a policy class may act as a base class, a pointer or reference to such
a policy class can be used to point or refer to the derived class using the
policy.

    This situation, although legal, should be avoided for various reasons:
        hi(pointer: to policy base class)
    itemization(
    it() Destruction of a derived class object using the base class's
destructor requires the implementation of a virtual destructor;
    it() A virtual destructor introduces overhead to a class that normally has
no data members, but merely defines behavior: suddenly a ti(vtable) is
required as well as a data member pointing to the vtable;
    it() Virtual member functions somewhat reduce the efficiency of code;
virtual member functions use emi(dynamic polymorphism), which in principle is
undoing the advantages of emi(static polymorphism) as offered by templates;
    it() Virtual member functions in templates may result in emi(code bloat):
once an instantiation of a class's member is required, the class's vtable and
em(all) its virtual members must be implemented too.
    )
    To avoid these drawbacks, it is good practice to em(prevent) the use of
references or pointers to policy classes to refer or point to derived
class objects. This is accomplished by providing policy classes with
        hi(destructor: for policy classes)
    em(non-virtual protected destructors). With a non-virtual destructor there
is no performance penalty and since its destructor is protected users cannot
refer to classes derived from the policy class using a pointer or reference to
the policy class.