Help and Support
 

powered byLive Search

FIX: C2555 On Virtual Functions with Covariant Return Types

Article ID:240862
Last Review:July 5, 2005
Revision:1.1
This article was previously published under Q240862
On This Page

SYMPTOMS

You can receive the following error message if you try to override a virtual function with a covariant return type:
error C2555: 'B::function' : overriding virtual function differs from'A::function' only by return type or calling convention
Please refer to the sample code in the "More Information" section for details.

Back to the top

CAUSE

The compiler does not support virtual functions with covariant return types as specified in the C++ Standard, section 10.3, which is quoted below: 5- The return type of an overriding function shall be either identical to the return type of the overridden function or covariant with the classes of the functions. If a function D::f overrides a function B::f, the return types of the functions are covariant if they satisfy the following criteria:
both are pointers to classes or references to classes
the class in the return type of B::f is the same class as the class in the return type of D::f or, is an unambiguous direct or indirect base class of the class in the return type of D::f and is accessible in D
both pointers or references have the same cv-qualification and the class type in the return type of D::f has the same cv-qualification as or less cv-qualification than the class type in the return type of B::f.

Back to the top

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

This problem was corrected in Microsoft Visual C++ .NET.

Back to the top

MORE INFORMATION

Steps to Reproduce Behavior

The following sample code demonstrates the error:
//test.cpp
// compiler option needed: none

#include "stdafx.h"

class A
     {
      public:
	virtual A* function();
     };
	
class B : public A
     {
      public:
	 B* function();
     };

Back to the top


APPLIES TO
Microsoft Visual C++, 32-bit Learning Edition 6.0
Microsoft Visual C++ 6.0 Professional Edition
Microsoft Visual C++ 5.0 Professional Edition
Microsoft Visual C++ 6.0 Enterprise Edition
Microsoft Visual C++ 5.0 Enterprise Edition

Back to the top

Keywords: 
kbbug kbfix kbnoupdate kbcpponly kbcompiler KB240862

Back to the top

Article Translations

 

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.