Forward declaration of nested types/classes in C++

前端 未结 7 1276
刺人心
刺人心 2020-11-22 10:45

I recently got stuck in a situation like this:

class A
{
public:
    typedef struct/class {...} B;
...
    C::D *someField;
}

class C
{
public:
    typedef          


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 11:04

    I would not call this an answer, but nonetheless an interesting find: If you repeat the declaration of your struct in a namespace called C, everything is fine (in gcc at least). When the class definition of C is found, it seems to silently overwrite the namspace C.

    namespace C {
        typedef struct {} D;
    }
    
    class A
    {
    public:
     typedef struct/class {...} B;
    ...
    C::D *someField;
    }
    
    class C
    {
    public:
       typedef struct/class {...} D;
    ...
       A::B *someField;
    }
    

提交回复
热议问题