By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,116 Members | 911 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,116 IT Pros & Developers. It's quick & easy.

Question about virtual

P: n/a
I have two classes. One with a regular destructor and the other with a
virtual destructor.
e.g.

class x {
~x(){}
}

vs

class y {
virtual ~x{}
}

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..

Thanks!

Dec 20 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* cw***@hotmail.com:
I have two classes. One with a regular destructor and the other with a
virtual destructor.
e.g.

class x {
~x(){}
}

vs

class y {
virtual ~x{}
}

Question:
1. is y likely to use more memory?
2. is the construct y likely to be slower?
Premature optimization (or pessimization) is evil.

Measure.

Then don't do it.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 20 '06 #2

P: n/a
cw***@hotmail.com wrote:
I have two classes. One with a regular destructor and the other with a
virtual destructor.
e.g.

class x {
~x(){}
}
;
>
vs

class y {
A destructor for y must be called ~y().

virtual ~x{}
}
;

Note that your destructors are private, so only members of the class itself
can destroy the object.
Question:
1. is y likely to use more memory?
Yes. On typical implementations, classes with virtual member functions get a
hidden pointer per object and one pointer per virtual member function, but
the latter only once for the class. But that will only be significant if
your program is either meant to run on e.g. very small microcontroller
architectures or you expect your program to contain at least a few hundred
thousand instances of your class.
2. is the construct y likely to be slower?
If at all, then I bet you won't be able to measure that difference.

Dec 20 '06 #3

P: n/a
cw***@hotmail.com wrote:
I have two classes. One with a regular destructor and the other with a
virtual destructor.
e.g.

class x {
~x(){}
}

vs

class y {
virtual ~y{} // fixed
}

Question:
1. is y likely to use more memory?
It is quite likely that the compiler will optimize away any differences
in the two classes.
2. is the construct y likely to be slower?
It does take longer to type...
as i understand, it is less efficient when calling y...
That isn't true at all. If the compiler can statically determine the
type of the object, the call will be as direct for the virtual function
as for any other function. If the compiler can't determine the type of
the object at runtime, then the call will be slower, but more efficient
because it will work (whereas the non-virtual function call wouldn't.)
but i m not sure about the memory and construction..
It's important to distinguish what exactly you are talking about here.
Do you mean the object code produced by the class or the amount of
memory an object of the class takes up?
Dec 20 '06 #4

P: n/a
cw***@hotmail.com wrote:
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++ :)

Dec 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.