Thread: Virtual & Pure virtual destructors

  1. #31
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I guess, unless you can find somewhere where they use pure virtual destructors.

  2. #32
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    here it is.....
    from Thinking in C++ by Bruce Eckel

    While pure virtual destructors are legal in Standard C++, there is an added constraint when using them: you must provide a function body for the pure virtual destructor. This seems counterintuitive; how can a virtual function be "pure" if it needs a function body? But if you keep in mind that constructors and destructors are special operations it makes more sense, especially if you remember that all destructors in a class hierarchy are always called. If you could leave off the definition for a pure virtual destructore, what function body would be called during destruction? Thus, it's absolutely necessary that the compiler and linker enforce the existence of a function body for a pure virtual destructor.

    If it's pure, but it has to have a function body, what's the value of it? The only difference you'll see between the pure and non-pure virtual destructor is that the pure virtual destructor does cause the base class to be abstract, so you cannot create an object of the base class (although this would also be true if any other member function of the base class were pure virtual).
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #33
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    I think this may help clear some things up
    http://www.objectmentor.com/resource...les/abcpvf.pdf

  4. #34
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    bah! my head hurts, I'm going to take a nap, go out to dinner, and come back tonight....

    ciao!

  5. #35
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    great thread. and nice new avatar bmj
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #36
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I'm arguing against your syntax:

    virtual ~classname() = 0;

    That is not proper syntax for a pure virtual destructor is it? I thought:

    virtual ~classname() {}

    Than just the methods have the syntax:

    virtual void method() = 0;

  7. #37
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    read the quote from Eckel that I posted above and then look at this code:
    Code:
    class test
         {
         public:
              test();
              virtual ~test()=0;
         };
    
    class test1 : public test
         {
         public:
              test1();
              ~test1();
         };
    
    test::test()
         {
         std::cout << "test()" << std::endl;
         }
    
    test1::test1()
         {
         std::cout << "test1()" << std::endl;
         }
    test::~test()
         {
         std::cout << "~test()" << std::endl;
         }
    
    test1::~test1()
         {
         std::cout << "~test1()" << std::endl;
         }
    This is how its supposed to be done. and yes, pure virtual functions have "= 0" at the end. It's very strange but the code for a pure virtual destructor DOES get called.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #38
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I don't believe this is true, that syntax doesn't look right.

  9. #39
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    copy and paste to your compiler. I already compiled it.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #40
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    The syntax is wrong. I swear. I'm reading The C++ Programming Language.

  11. #41
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    then I guess Thinking in C++ is wrong then right?

    virtual ~classname(){};

    is not pure. It's virtual but not pure. I don't know what your book says but trust me on this one.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  12. #42
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I've consulted 3 books so far and I have not seen that syntax for a destructor in an abstract class.

  13. #43
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Thinking in C++ second edition... page 668
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  14. #44
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by Troll_King
    I've consulted 3 books so far and I have not seen that syntax for a destructor in an abstract class.
    An abstract base class doesn't have to have ALL methods pure virtual. Just one. The abstract class examples you are seeing don't have to have pure virtual destructors.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  15. #45
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Okay I found the answer in Effective C++. It says that you can declare a pure virtual destructor in that mannor if you don't have any functions in the class. BUT you MUST provide the definition for the pure virtual destructor. Thats why your code works, but it is rather unorthadox. It is much better to use the normal way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Information Regarding Pure Virtual Functions
    By shiv_tech_quest in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2003, 04:43 AM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM
  5. virtual or pure virtual
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-01-2001, 07:19 PM