Topic: Pure virtual destructors and definitions


Author: "Chad Hoersten" <chadh@entekird.com>
Date: 2000/08/29
Raw View
I came across code similar to the code below that confused me.  I found in
the C++ FAQ Lite that it is legal declare and define pure virtual methods in
the same class.  I don't see where it would generally be of much use.  I'm
also wondering at the legality/usefulness of the pure virtual destructor.

Chad Hoersten

---------8<----------
// 1 file that compiles under VC 6 and gcc
#include <iostream>
// Declaration of abstract base class
class ABC
    {
    public:
        virtual ~ABC() = 0;
        virtual bool foo() = 0;
    };

// Definition of pure virtual methods
ABC::~ABC()
    {
    std::cout << "~ABC\n";
    }

bool ABC::foo()
    {
    std::cout << "ABC::foo\n";
    return true;
    }

// Declaration of derived class using ABC's definitions
class C : public ABC
    {
    public:
        bool foo() { return ABC::foo(); }
    };

// Declaration of derived class overriding ABC's definitions
class D : public ABC
    {
    public:
        bool foo();
    };

// Definition
bool D::foo()
    {
    std::cout << "D::foo\n";
    return false;
    }

int main()
    {
    D d;
    d.foo();
    C c;
    c.foo();
    return 0;
    }


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Danil <danil@ultranet.com>
Date: 2000/08/30
Raw View
In article <39aaccc1_2@news2.one.net>, chadh@entekird.com says...
> I came across code similar to the code below that confused me.  I found in
> the C++ FAQ Lite that it is legal declare and define pure virtual methods in
> the same class.  I don't see where it would generally be of much use.  I'm
> also wondering at the legality/usefulness of the pure virtual destructor.

See Item 36 "Differentiate between inheritance of interface and
inheritance of implementation" - _Effective C++_, S. Meyers.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]