Class has virtual member functions but no derived classes

A class has virtual member functions, but no other class is derived from that class.

A virtual member function can be overridden by a definition in a derived class. If no other class is derived from this class then this flexibility is unused. Calls to virtual member functions are somewhat more expensive than calls to non-virtual member functions, so a performance gain may be derived from removing the virtual keyword from member functions in these classes.

Note that one might wish to designate a member function as virtual even though there are currently no classes derived from this class if you expect that you would want to derive from that class in the future (or in other contexts that reuse this code). In that case, it may be awkward to remove the virtual keyword in some contexts and not in others, in which case this warning can be ignored.

ID

Observation

Description

1

Definition

The place the class was defined

Examples

          
class A {
public:
    virtual void f()
    {
    }

    virtual ~A() throw()
    {
    }
};

int
main()
{
    A *pa = new A;
    
    // This call will go through vtable because f is declared virtual.
    // Since A is never used as a base class, there is no benefit
    // to declaring f as virtual.  If f were not declared virtual
    // this would compile into a faster direct call.
    pa->f();
    
    delete pa;
    return 0;
}
        

Copyright © 2010, Intel Corporation. All rights reserved.