jeudi 23 mai 2019

Is a forward declaration inside a class body considered a different type?

I have two inner structs inside an enclosing class and in one of the structs, I have a pointer member to objects of the other struct's type. And to improve readability and make it clear that the forward declaration is intended for the struct that needs it, I put the forward declaration in the inner struct itself. Something like this

class Enclosing{
public:
    struct InnerA{
       struct InnerB; // forward declaration inside InnerA to improve readability  
       InnerB* b; 
       // other members
   };

    struct InnerB{

       // lots of member variables
    };
};

And then somewhere outside I have a function

void DoSomething(){
    Enclosing::InnerA a;


    // error incompatible types Enclosing::InnerB* and Enclosing::InnerA::InnerB*
    Enclosing::InnerB* ptr = a.b; 
}

It is to my understanding that forward declarations are only a way to tell the compiler that a class exists, not define a completely different new type. Is this standard? if so, is there a way to have the forward declaration inside the struct without being considered as a different type?

Aucun commentaire:

Enregistrer un commentaire