I guess, unless you can find somewhere where they use pure virtual destructors.
I guess, unless you can find somewhere where they use pure virtual destructors.
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
I think this may help clear some things up
http://www.objectmentor.com/resource...les/abcpvf.pdf
bah! my head hurts, I'm going to take a nap, go out to dinner, and come back tonight....
ciao!
great thread. and nice new avatar bmj
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
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;
read the quote from Eckel that I posted above and then look at this code:
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.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; }
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
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
The syntax is wrong. I swear. I'm reading The C++ Programming Language.
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
I've consulted 3 books so far and I have not seen that syntax for a destructor in an abstract class.
Thinking in C++ second edition... page 668
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
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.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.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
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.