Question about virtual

C

cwc5w

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!
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
R

Rolf Magnus

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

Daniel T.

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?
 
G

Grizlyk

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++ :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top