Visual Studio 2008を使用していて、親と子の2つのクラスがあります。親はヘッダーでいくつかの静的const変数を宣言し、それらはcppファイルで定義されます。子クラスのswitchステートメントでdefinesascaseを使用しようとすると、次のエラーが発生します。C2051:case式が定数ではありません。だから私はいくつかのテストをしました、そして私が見ている振る舞いは幾分一貫性がありません。
// Parent.h
class Parent
{
public:
Parent();
~Parent(void) { }
static const unsigned long A = 1;
static const unsigned long B;
};
// Parent.cpp
#include "Parent.h"
const unsigned long Parent::B = 2;
Parent::Parent()
{
// Everything works fine here
unsigned long l;
switch(l)
{
case A:
break;
case B:
break;
default:
break;
}
}
// Child.h
#pragma once
#include "Parent.h"
class Child :
public Parent
{
public:
Child(void);
virtual ~Child(void) { }
static const int C = 3;
static const int D;
};
// Child.cpp
#include "Child.h"
const int Child::D = 4;
Child::Child(void)
{
unsigned long l;
switch(l)
{
case A:
break;
case B: // C2051: case expression not constant
break;
case C:
break;
case D:
break;
default:
break;
}
}
また、直接指定してみましParent::B
たが、問題は解決しません。変数が親クラスから継承されている場合を除いて、すべての場合に式が一定である理由はありますか?