Skip to content

Commit 9faaef6

Browse files
authored
Merge pull request #2775 from corob-msft/cr-c2555
Fix 2036 add explanation in C2555
2 parents 1f0018b + 1ea8930 commit 9faaef6

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

docs/error-messages/compiler-errors-2/compiler-error-c2555.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
---
22
title: "Compiler Error C2555"
3-
ms.date: "11/04/2016"
3+
description: "Reference for Visual Studio C++ compiler error C2555."
4+
ms.date: "03/30/2020"
45
f1_keywords: ["C2555"]
56
helpviewer_keywords: ["C2555"]
67
ms.assetid: 5e49ebb8-7c90-457a-aa12-7ca7ab6574b2
78
---
89
# Compiler Error C2555
910

10-
' class1::function1': overriding virtual function return type differs and is not covariant from 'class2::function2'
11+
> '*class1*::*function1*': overriding virtual function return type differs and is not covariant from '*class2*::*function2*'
1112
12-
A virtual function and a derived overriding function have identical parameter lists but different return types. An overriding function in a derived class cannot differ from a virtual function in a base class only by its return type.
13+
A virtual function and a derived overriding function have identical parameter lists but different return types.
1314

14-
To resolve this error, cast the return value after the virtual function has been called.
15+
## Remarks
1516

16-
You may also see this error if you compile with /clr. For example, the Visual C++ equivalent to the following C# declaration:
17+
In C++, an overriding function in a derived class can't differ only by return type from a virtual function in a base class.
1718

18-
```
19+
There's an exception to this rule for certain return types. When a derived class overrides a public base class, it may return a pointer or reference to the derived class instead of a base-class pointer or reference. These return types are called *covariant*, because they vary together with the type. This rule exception doesn't allow covariant reference-to-pointer or pointer-to-pointer types.
20+
21+
One way to resolve the error is to return the same type as the base class. Then, cast the return value after the virtual function has been called. Another is to also change the parameter list, to make the derived class member function an overload instead of an override.
22+
23+
## Examples
24+
25+
You may see this error if you compile with **`/clr`**. For example, the C++ equivalent to the following C# declaration:
26+
27+
```csharp
1928
Guid[] CheckSources(Guid sourceID, Guid[] carouselIDs);
2029
```
2130

2231
is
2332

24-
```
33+
```cpp
2534
Guid CheckSources(Guid sourceID, Guid carouselIDs[]) [];
2635
```
2736
@@ -38,3 +47,5 @@ struct Y : X {
3847
void func2(); // OK
3948
};
4049
```
50+
51+
To fix it, change the return type of `Y::func` to `void`.

0 commit comments

Comments
 (0)