1

virtual として定義された基本クラスの純粋仮想関数がありますint GetData() const = 0

各派生クラスで列挙型を定義し、GetData 関数の戻り値 (派生クラス固有の列挙型) 値をオーバーライドしようとします。

例えば:

class Derived1 : public Base
{
public :
enum D1
{
   d1_1 = 0,
   d1_2 = 60,
   ...
   d1_100
};
D1 GetData () const;
};
class Derived2 : public Base
{
public :
enum D2
{
   d2_1 = 10,
   d2_2 = 39,
   ...
   d2_300
};
D2 GetData () const;
};

すべてのクラスのすべての列挙値に対して同じ範囲を定義することはできないと言うのは非常に重要です。上記のコードは、コンパイル エラーを生成します。

error C2555: : overriding virtual function return type differs and is not covariant

アドバイス - どうすれば解決できますか?

4

4 に答える 4

2

特定のケースでは、メソッドがプリミティブ型を返すという事実があります。これは、C#virtualのような一般的な型に配置できないため、共分散がありません。 共分散を満たすには、すべての戻り値の型の基本クラスとして機能するクラスを定義する必要があります。System.Object

ウィキペディアから:

Within the type system of a programming language, covariance and contravariance refers to the
ordering of types from narrower to wider and their interchangeability or equivalence in certain 
situations (such as parameters, generics, and return types).

covariant: converting from a specialized type (Cats) to a more general type (Animals): 
Every cat is an animal.

記事へのリンクはこちらです。

于 2013-04-07T19:04:26.743 に答える