Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pure virtual function calls / optimization

Reply
Thread Tools

pure virtual function calls / optimization

 
 
alexander.stippler@uni-ulm.de
Guest
Posts: n/a
 
      07-27-2006
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

 
Reply With Quote
 
 
 
 
alexander.stippler@uni-ulm.de
Guest
Posts: n/a
 
      07-27-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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
{
//...
};

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-27-2006
(E-Mail Removed) 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.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-27-2006
(E-Mail Removed) 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?

 
Reply With Quote
 
alexander.stippler@uni-ulm.de
Guest
Posts: n/a
 
      07-27-2006

Rolf Magnus schrieb:

> (E-Mail Removed) 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

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-27-2006
(E-Mail Removed) wrote:

>
> Rolf Magnus schrieb:
>
>> (E-Mail Removed) 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.

 
Reply With Quote
 
alexander.stippler@uni-ulm.de
Guest
Posts: n/a
 
      07-27-2006

Rolf Magnus schrieb:

> (E-Mail Removed) wrote:
>
> >
> > Rolf Magnus schrieb:
> >
> >> (E-Mail Removed) 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

 
Reply With Quote
 
Mike Smith
Guest
Posts: n/a
 
      07-27-2006
(E-Mail Removed) wrote:
> Rolf Magnus schrieb:
>
>> (E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need clarification for virtual method and pure virtual function a C++ 7 06-28-2007 12:38 PM
virtual vs pure virtual member function sam_cit@yahoo.co.in C++ 7 05-02-2007 10:00 AM
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM
This function has an onClick event that calls a function that calls This function Bob Javascript 5 10-24-2006 04:11 PM
virtual function and pure virtual function in the implementation of COM IK C++ 2 07-23-2004 02:55 PM



Advertisments