简体   繁体   English

我不明白这个结果

[英]I don't understand this result

This code doesn't compile in clang这段代码不能在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';
}

But if I comment out /*: public M::B*/ it does, printing 1. However, if I place the statement static const int i = 1;但是如果我注释掉/*: public M::B*/它确实会打印 1。但是,如果我放置语句static const int i = 1; before int a[i];int a[i]; it compiles on both versions of the code, printing 1.它在两个版本的代码上编译,打印 1。

Would very much appreciate quotes from the Standard.非常感谢标准中的报价。

In the original code with the commented base class you are trying to use name i that was not yet defined在带有注释基类的原始代码中,您尝试使用尚未定义的名称i

   class X {
        int a[i]; // here i is undefined
        static const int i = 1;

If you will exchange the two definitions如果您将交换两个定义

   class X {
        static const int i = 1;
        int a[i];

then the code will be compiled successfuly because i used in the array definition was previously defined..那么代码将被成功编译,因为我在数组定义中使用的是以前定义的..

When base class is uncommented then in this definition当基类被取消注释时,则在此定义中

        int a[i];

i is the static data member of class B that is the array is defined as i 是类 B 的静态数据成员,即数组定义为

        int a[1000];

After that you defined static data member i of class X that hides static data member of base class B of class Y inside class X.之后,您定义了类 X 的静态数据成员 i,它将类 Y 的基类 B 的静态数据成员隐藏在类 X 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM