473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Question about virtual

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
4 2121
* 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
3
by: danra | last post by:
Hi, I have a question which seems to me pretty basic, unfortunately I can't seem to figure it out. Let's say I have an abstract base class Vehicle and classes Car and Truck which derive from...
31
by: grahamo | last post by:
This came up in an interview I did a while ago and I wanted to know the correct answer. The setup is this; If I have a base class "food" and also two classes "meat" and "veg" that inherit from...
4
by: aap | last post by:
Hi, I have the following code. #include <iostream> using namespace std; class Root { public: virtual void Root1() = 0; virtual void Root2() = 0;
5
by: gouqizi.lvcha | last post by:
Hi, all: I have 3 class X, Y, Z class Y is a subclass of class X; class Z is a subclass of class Y; i.e. class Y : public class X class Z : public class Y
8
by: Hardy Wang | last post by:
Hi: Is it possible for me to create/open web application from remote machine other than port 80? And create application directly under virtual web site instead of creating a virtual directory?...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
5
by: Amal P | last post by:
Dears, I have a question. class Data { }; class DisableDerive:virtual protected Data // A base class { private:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.