Why is the declaration not needed for the concept of a class of friends?

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 &paramFE = 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.

+10
c ++ class forward-declaration friend-class
Mar 27 2018-12-12T00:
source share
4 answers

You're right, a friend’s announcement is like a declaration.

The following compilations:

 class A; class B { friend A; }; 

or

 class B { friend class A; }; 

this is not true:

 class B { friend A; }; 

This is actually not a friend declaration, which forward-declares class A , but the class keyword. This is why the second example does not work, because it does not know what A . If you declare A in advance, as in the first fragment, it may allow A declare the class.

I stand fixed.

+6
Mar 27 2018-12-12T00:
source share
 friend class TLS; 

This syntax is a declaration in itself, so you don't need an additional previous type declaration. Note that the friend declaration is slightly different (especially for functions) than the declaration in the encompassing namespace.

In particular, if there is no declaration in the encompassing namespace, the function declared in the friend declaration can only be found using a search dependent on the argument (and cannot be defined outside the class). The same applies to classes, if there is no declaration at the namespace level, the declared type will not be accessible outside the class, declaring it as a friend.

 class B { friend class A; }; //A foo(); // Error: A is not declared here! class A; A foo(); // Fine 
+7
Mar 27 '12 at 13:48
source share

forward declaration should not be at the top of the file, as shown below.

 class A; class C; class D; class B { A* a; C* c; D* d; }; 

coincides with

 class B { class A* a; class C* c; class D* d; }; 

friend recommended syntax only uses

+2
Mar 27 2018-12-12T00:
source share

Does this mean that declaring a friend's class is a direct declaration of everything in it itself?

Yes

0
Mar 27 '12 at 13:48
source share



All Articles