pure virtual function calls / optimization

A

alexander.stippler

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
 
A

alexander.stippler

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
{
//...
};
 
I

Ian Collins

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

Rolf Magnus

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

alexander.stippler

Rolf said:
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
 
R

Rolf Magnus

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

alexander.stippler

Rolf said:
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
 
M

Mike Smith

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.
 

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,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top