Can a member of a class be named the same name as its type (another class)?

前端 未结 3 2031
半阙折子戏
半阙折子戏 2020-12-20 12:50

Trying to compile the following code on different compilers gives me two different results:

struct S{};
struct T{S S;};
int main(){}

As you

3条回答
  •  一生所求
    2020-12-20 13:26

    @JesseGood provide a complete answer, but if you really want to do this without any error, you can use type's full name and it will work as follow:

    struct S {};
    struct T { ::S S; };
    int main() {return 0;}
    

    No there is no error, since S in your class is T::S and its type is ::S!

提交回复
热议问题