I have two classes. One with a regular destructor and the other with a
virtual destructor.
Question:
1. is y likely to use more memory?
2. is the construct y likely to be slower?
as i understand, it is less efficient when calling y...but i m not sure
about the memory and construction..
I think, practical you must select "virtual" or "not virtual" only for
design purpose, so do not pay any attention to "efficient" while
designing classes. The "efficient" is not question, instead of it, you
question must be "what design way I must use for the class", and the
answer for the "design way" is answer for "virtual" or "not virtual".
For the question of virtual destructor usage and virtual function
implementation (memory size, slow/fast), the newsgroup request the next
answer :
This issue is covered in the C++ FAQ.
You can get the FAQ at:
www.parashift.com/c++-faq-lite/
Please read the FAQ.
But the FAQ is enough large but maybe not very complete or you can do
not have good "http" access to internet, so i can give a short answer:
Virtual destructor is the same as ordinary virtual function member -
all virtuals are used for polymorphic class behaviour ( for access to
object over pointer or reference to its base class ).
That is why, If you are going to delete object with the help of pointer
to its base class (it is nearly always happened when you are using
object with the help of pointer to its base class), you must declare
destructor as virtual (C++ compilers even print warning for this case).
Each local virtual stuffs is always more slow and use more memory than
not virtual local stuff, but for good systems of classes, all virtuals
together (due to virtual) allows to reduce total memory size, than that
for not virtual.
I will not speak about the profit of virtual stuffs for reducing wasted
time and wasted forces while you are programming.
In most cases, you can not increase total speed with the help of
virtual, because ordinary functions, inlined functions, parameters in
registers, expanded for() and while() operators and many other
low-level local acceleration ways always have better speed. But in good
systems of classes virtual stuffs do not give noticable decreasing of
total speed.
Each class with virtuals has single runtime copy of table of virtual
functions - all objects of the class are sharing single virtual table,
so you can have any number of virual function without memory leak. And
so on. Read C++ books for details, i do not remember, for "efficient"
it is not so big trouble for C++