pure virtual function calls / optimization

Discussion in 'C++' started by alexander.stippler@uni-ulm.de, Jul 27, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Guest

    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, 2006
    #2
    1. Advertising

  3. Ian Collins Guest

    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.
    Ian Collins, Jul 27, 2006
    #3
  4. Rolf Magnus Guest

    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?
    Rolf Magnus, Jul 27, 2006
    #4
  5. Guest

    Rolf Magnus schrieb:

    > 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, 2006
    #5
  6. Rolf Magnus Guest

    wrote:

    >
    > Rolf Magnus schrieb:
    >
    >> 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.
    Rolf Magnus, Jul 27, 2006
    #6
  7. Guest

    Rolf Magnus schrieb:

    > wrote:
    >
    > >
    > > Rolf Magnus schrieb:
    > >
    > >> 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, 2006
    #7
  8. Mike Smith Guest

    wrote:
    > Rolf Magnus schrieb:
    >
    >> 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
    Mike Smith, Jul 27, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. IK
    Replies:
    2
    Views:
    605
    hemraj
    Jul 23, 2004
  2. John Goche
    Replies:
    10
    Views:
    735
    Marcus Kwok
    Dec 8, 2006
  3. Replies:
    7
    Views:
    586
    James Kanze
    May 2, 2007
  4. a
    Replies:
    7
    Views:
    361
    dasjotre
    Jun 28, 2007
  5. Bob
    Replies:
    5
    Views:
    259
Loading...

Share This Page