473,406 Members | 2,369 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,406 software developers and data experts.

pure virtual function calls / optimization

Hi

I wonder if their will be a performance penalty in the following
situation due to virtual function calls:

class Interface
{
public:
virtual void methodA() = 0;
virtual void methodB() = 0;
virtual void methodC() = 0;
virtual void methodD() = 0;
};

class Implementation
{
public:
void methodA() { /* do someting */ }
void methodB() { /* do someting */ }
void methodC() { /* do someting */ }
void methodD() { /* do someting */ }

};

IMHO there is no need for class Implementation to have a virtual method
table, since there is just one possibility for every method. It's a
special case, I know. Other derived classes may have virtual functions.
What do current compilers do? Are there optimizations for such
situations or did I just miss something?

regards,
Alex

Jul 27 '06 #1
7 2109

al****************@uni-ulm.de schrieb:
Hi

I wonder if their will be a performance penalty in the following
situation due to virtual function calls:

class Interface
{
public:
virtual void methodA() = 0;
virtual void methodB() = 0;
virtual void methodC() = 0;
virtual void methodD() = 0;
};

class Implementation
{
public:
void methodA() { /* do someting */ }
void methodB() { /* do someting */ }
void methodC() { /* do someting */ }
void methodD() { /* do someting */ }

};

IMHO there is no need for class Implementation to have a virtual method
table, since there is just one possibility for every method. It's a
special case, I know. Other derived classes may have virtual functions.
What do current compilers do? Are there optimizations for such
situations or did I just miss something?

regards,
Alex
Sorry, of course the Implementation class is publicly derived from the
Interface class.

class Implementation
: public Interface
{
//...
};

Jul 27 '06 #2
al****************@uni-ulm.de wrote:
Hi

I wonder if their will be a performance penalty in the following
situation due to virtual function calls:

class Interface
{
public:
virtual void methodA() = 0;
virtual void methodB() = 0;
virtual void methodC() = 0;
virtual void methodD() = 0;
};

class Implementation
{
public:
void methodA() { /* do someting */ }
void methodB() { /* do someting */ }
void methodC() { /* do someting */ }
void methodD() { /* do someting */ }

};

IMHO there is no need for class Implementation to have a virtual method
table, since there is just one possibility for every method. It's a
special case, I know. Other derived classes may have virtual functions.
What do current compilers do? Are there optimizations for such
situations or did I just miss something?
The may well be an over head, but it will be small. Whether or not the
compiler can perform any optimisations such as inlining depends on the
compiler and the use of the functions.

--
Ian Collins.
Jul 27 '06 #3
al****************@uni-ulm.de wrote:
Hi

I wonder if their will be a performance penalty in the following
situation due to virtual function calls:

class Interface
{
public:
virtual void methodA() = 0;
virtual void methodB() = 0;
virtual void methodC() = 0;
virtual void methodD() = 0;
};

class Implementation
{
public:
void methodA() { /* do someting */ }
void methodB() { /* do someting */ }
void methodC() { /* do someting */ }
void methodD() { /* do someting */ }

};

IMHO there is no need for class Implementation to have a virtual method
table, since there is just one possibility for every method.
If that is the whole program. There might be classes derived from
Implementation. If you access an object of such a derived class through a
pointer or reference to Implementation, how would the compiler know which
function to call?

Jul 27 '06 #4

Rolf Magnus schrieb:
al****************@uni-ulm.de wrote:
Hi

I wonder if their will be a performance penalty in the following
situation due to virtual function calls:

class Interface
{
public:
virtual void methodA() = 0;
virtual void methodB() = 0;
virtual void methodC() = 0;
virtual void methodD() = 0;
};

class Implementation
{
public:
void methodA() { /* do someting */ }
void methodB() { /* do someting */ }
void methodC() { /* do someting */ }
void methodD() { /* do someting */ }

};

IMHO there is no need for class Implementation to have a virtual method
table, since there is just one possibility for every method.

If that is the whole program. There might be classes derived from
Implementation. If you access an object of such a derived class through a
pointer or reference to Implementation, how would the compiler know which
function to call?
The members of Implementation are no longer virtual. So this should be
clear.

Alex

Jul 27 '06 #5
al****************@uni-ulm.de wrote:
>
Rolf Magnus schrieb:
>al****************@uni-ulm.de wrote:
Hi

I wonder if their will be a performance penalty in the following
situation due to virtual function calls:

class Interface
{
public:
virtual void methodA() = 0;
virtual void methodB() = 0;
virtual void methodC() = 0;
virtual void methodD() = 0;
};

class Implementation
{
public:
void methodA() { /* do someting */ }
void methodB() { /* do someting */ }
void methodC() { /* do someting */ }
void methodD() { /* do someting */ }

};

IMHO there is no need for class Implementation to have a virtual method
table, since there is just one possibility for every method.

If that is the whole program. There might be classes derived from
Implementation. If you access an object of such a derived class through a
pointer or reference to Implementation, how would the compiler know which
function to call?

The members of Implementation are no longer virtual. So this should be
clear.
You're mistaken. If a member function of a base class is virtual, that
member function is automatically virtual in every derived class, even if
not explicitly specified.

Jul 27 '06 #6

Rolf Magnus schrieb:
al****************@uni-ulm.de wrote:

Rolf Magnus schrieb:
al****************@uni-ulm.de wrote:

Hi

I wonder if their will be a performance penalty in the following
situation due to virtual function calls:

class Interface
{
public:
virtual void methodA() = 0;
virtual void methodB() = 0;
virtual void methodC() = 0;
virtual void methodD() = 0;
};

class Implementation
{
public:
void methodA() { /* do someting */ }
void methodB() { /* do someting */ }
void methodC() { /* do someting */ }
void methodD() { /* do someting */ }

};

IMHO there is no need for class Implementation to have a virtual method
table, since there is just one possibility for every method.

If that is the whole program. There might be classes derived from
Implementation. If you access an object of such a derived class through a
pointer or reference to Implementation, how would the compiler know which
function to call?
The members of Implementation are no longer virtual. So this should be
clear.

You're mistaken. If a member function of a base class is virtual, that
member function is automatically virtual in every derived class, even if
not explicitly specified.
ah ok. I did not know this. So just forget the whole question. It does
no longer make sense.
Thanks

Alex

Jul 27 '06 #7
al****************@uni-ulm.de wrote:
Rolf Magnus schrieb:
>al****************@uni-ulm.de wrote:
>>Hi

I wonder if their will be a performance penalty in the following
situation due to virtual function calls:

class Interface
{
public:
virtual void methodA() = 0;
virtual void methodB() = 0;
virtual void methodC() = 0;
virtual void methodD() = 0;
};

class Implementation
{
public:
void methodA() { /* do someting */ }
void methodB() { /* do someting */ }
void methodC() { /* do someting */ }
void methodD() { /* do someting */ }

};

IMHO there is no need for class Implementation to have a virtual method
table, since there is just one possibility for every method.
If that is the whole program. There might be classes derived from
Implementation. If you access an object of such a derived class through a
pointer or reference to Implementation, how would the compiler know which
function to call?

The members of Implementation are no longer virtual. So this should be
clear.
They were declared virtual in Interface, and would therefore still be
virtual in Implementation. You can't "un-virtualize" a function.

--
Mike S
Jul 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: IK | last post by:
Hello All, Please excuse me for posting this here, but I don't find any other group where I will get a proper answer. This is about clarifying the C++ part of COM. I understand that COM is a...
2
by: Derek | last post by:
The following simple program causes a runtime error (pure virtual method called): struct Base { Base() { init(); } void init() { badIdea(); } virtual void badIdea() = 0; };
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
13
by: junky_fellow | last post by:
I have read certain articles that encourage to use/write pure functions (if possible) as they are better suited for optimization. I got one example that expalins how the code can be optimised....
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
7
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to what is the exact difference between a virtual function and a pure virtual function? Thanks in advance!!!
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.