[英]I don't understand this result
這段代碼不能在clang 中編譯
#include <iostream>
namespace M {
class B {
public:static const int i = 1000;
};
}
namespace N {
class Y /*: public M::B*/ {
class X {
int a[i];
static const int i = 1;
public:
X() { for(int j = 0; j < i; ++j ) a[j] = 1; }
static int Get() { return i; }
};
public:
int Get() { return X::Get(); }
};
}
int main()
{
N::Y y;
std::cout << y.Get() << '\n';
}
但是如果我注釋掉/*: public M::B*/
它確實會打印 1。但是,如果我放置語句static const int i = 1;
在int a[i];
它在兩個版本的代碼上編譯,打印 1。
非常感謝標准中的報價。
在帶有注釋基類的原始代碼中,您嘗試使用尚未定義的名稱i
class X {
int a[i]; // here i is undefined
static const int i = 1;
如果您將交換兩個定義
class X {
static const int i = 1;
int a[i];
那么代碼將被成功編譯,因為我在數組定義中使用的是以前定義的..
當基類被取消注釋時,則在此定義中
int a[i];
i 是類 B 的靜態數據成員,即數組定義為
int a[1000];
之后,您定義了類 X 的靜態數據成員 i,它將類 Y 的基類 B 的靜態數據成員隱藏在類 X 中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.