I recently learned about the concept of friend class in C ++ (I was looking for a little language, but this answer made me laugh until I remembered the most important parts), and I'm trying to include it in the project I'm working on. A specific question is highlighted at the end, but in general I am confused by the complete absence of forward declarations in my working code.
All my classes are separated through (sub) folders and each into a separate .h and .cpp file, but this should be enough to get an idea of the dependencies:
// FE.h - no implementations - no .cpp file class FE { private: virtual void somePrivateFunc() = 0; // 90% virtual class, interface for further implementations friend class TLS; }; // DummyFE.h #include "FE.h" class DummyFE :: public FE { /* singleton dummy */ private: // constructor public: static DummyFE& instance(); }; // DummyFE.cpp #include "DummyFE.h" // all Dummy FE implementation // ImplFE.h #include "FE.h" class ImplFE :: public FE { /* implemented */ }; // ImplFE.cpp #include "FE.cpp" // all Impl FE implementations // SD.h - implements strategy design pattern // (real project has more than just FE class in here) #include "FE.h" #include "DummyFE.h" class SD { private: FE &localFE; public: SD(FE ¶mFE = DummyFE::instance()); // ... and all the other phun stuff ... friend class TLS; }; // SD.cpp - implementations # include "SD.h" /* SD implemented */ // TLS.h - implements strategy design pattern (on a higher level) #include SD.h class TLS{ private: SD *subStrategy; public: void someFunctionRequiringFriendliness(); } // TLS.cpp - implementations #include "TLS.h" void TLS::someFunctionRequiringFriendliness(){ this->subStrategy->localFE.somePrivateFunc(); // ok! }
Now I had a party that collected all this for compilation with all the dependencies (I had to write it to the class diagram in order for it to work), but now it happens. The fact that I am really confused is that advanced declarations are not needed. I know about forward announcements before, and just in case, I updated my memory with this answer .
So, to try to keep this clear, my question is : When declaring a class TLS as a friend, why aren't there explicit forward declarations? Does this mean that the friend class declaration is a declaration of transition to it? For me, intuitively, something is missing here ... And since it compiles and works fine, can someone help me fix my intuition ?: D
PS sorry for such a long introduction to the question and a bunch of code. Please do not comment on my code concept - friends are good here, I am sure that this is correct for my current project (it is a little difficult to see from this skeleton). I just wanted to know why there was no need for a forward declaration anywhere.
c ++ class forward-declaration friend-class
penelope Mar 27 2018-12-12T00: 00Z
source share