Diagnostic 357: only nonstatic member functions may be virtual

Submit New Article

Last Modified On :   November 17, 2008 2:40 PM PST
Rate
 


Cause:

A virtual function cannot be a static member, since a virtual function call relies on a specific object (base or derived) for determining which function to invoke (note: ISO C++ standard); whereas Static functions are object independent.

Default type of this diagnostic is warning. (Microsoft* Visual C++*'s equivalent  warning is C4526.)

Example:

class base {
virtual void  foo( int ) = 0;
};

class derived: public base {  
static void  foo( int ); // generates warning 357
};

 >>icl   /c   t.cpp
Intel(R) C++ Compiler Professional for applications running on IA-32, Version 11
.0    Build 20080916 Package ID: w_cproc_p_11.0.061
Copyright (C) 1985-2008 Intel Corporation.  All rights reserved.

t.cpp
t.cpp(11): warning #357: only nonstatic member functions may be virtual
  static void  foo( int );
                     ^

 

Possible resolution:

  • Remove the static qualifier.
  • Rename the static function name in derived class, i.e. rename "foo" to "bar".

     

 





This article applies to: Intel® C++ Compiler for Linux* Knowledge Base,   Intel® C++ Compiler for Mac OS X* Knowledge Base,   Intel® C++ Compiler for Windows* Knowledge Base,   Intel® Parallel Composer Knowledge Base