How come forward declaration is not needed for friend class concept?
Asked Answered
F

4

12

I've just recently learned about friend class concept in C++ (I've googled around for a bit, but this answer made me laugh until I remembered the most important parts), and I'm trying to incorporate it in to the project I'm working on right now. The concise question is singled out in the end, but in general, I'm confused by complete lack of forward declarations in my working code.

All of my classes are separated through (sub-)folders and each one into a separate .h and .cpp file, but this should be enough to get a feeling about 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've had party getting all of this to actually compile with all the dependencies (had to write it down in to a class diagram in the end to make it work), but now it does. The fact that is actually confusing me, is that no forward declarations were needed. I know about forward declarations from before, and just in case, I refreshed my memory with this answer.

So, to try and keep it clear, my question: When declaring the class TLS as a friend, how come no explicit forward declarations were needed? Does that mean that a friend class declaration is a forward declaration all in it self? For me, intuitively, something here is missing... And since it compiles and works normally, can somebody help correct my intuition? :D

PS sorry for such a lengthy introduction to the question and a bunch of code. Please, don't comment on my code concept - friends are good here, I'm pretty sure it's correct for my current project (it's just a bit hard to see from this skeleton). I'd just like to know why no forward declaration was needed anywhere.

Frore answered 27/3, 2012 at 13:43 Comment(0)
B
7

You're right, the friend declaration is kind of like a forward declaration.

The following compiles:

class A;
class B
{
   friend A;
};

or

class B
{
   friend class A;
};

this does not:

class B
{
   friend A;
};

It's not actually the friend declaration that forward-declares class A, but the class keyword. That's why the second example doesn't work, because it doesn't know what A is. If you declare A beforehand, like in the first snippet, it can resolve A to a class declaration.

I stand corrected.

Breaker answered 27/3, 2012 at 13:47 Comment(4)
Hm. Strange. I have just the situation you said does not compile in my version of the code (it is reflected here by the fact that FE does not forward declare TLS, or include it at all) - and it works! That's what's confusing me! Ah, and also, I just noticed you used the syntax 'friend A' instead of 'friend class A'. Could you expand on that, did I accidentally manage to merge the forward and friend declaration into one?Frore
Note: The first snippet will not compile in C++03 (for those that cannot yet use C++11). The second snippet does not compile with g++4.5 or clang++3.0 (even in C++11 mode).Pat
@Luchian I am confused how you compiled your second example? This prompted me to ask this question which seems to indicate your second example shouldn't compile: #14115456Antenatal
@Antenatal interesting, thanks so much for pointing this out. You learn something new every day :)Breaker
S
7
friend class TLS;

This syntax is a declaration in itself, that is why you don't need an extra previous declaration of the type. Note that the friend declaration is slightly different (specially for functions) than a declaration in the enclosing namespace.

In particular, unless there is also a declaration in the enclosing namespace, a function declared in a friend declaration can only be found by argument dependent lookup (and cannot be defined outside of the class). The same goes for classes, unless there is also a declaration at namespace level, the type so declared will not be available outside of 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
Subcortex answered 27/3, 2012 at 13:48 Comment(2)
I was just wondering how come I did not need to explicitly declare TLS in FE and SD but this is a lot of new info. What you suggested (declaring types only in another class' body), would probably be used to implement class' internal utility functions or some such?Frore
@penelope: I think it is just a side effect of how lookup is performed. I haven't given it much thought regarding types, where it seems to mean there is a type A that has access to this class, and nothing else (i.e. You cannot use A as a proper type even inside B). But in the case of functions and specifically if you define the function inside the class definition it impacts lookup quite a bit.Pat
S
3

Does that mean that a friend class declaration is a forward declaration all in it self?

Yes

Strahan answered 27/3, 2012 at 13:48 Comment(0)
S
3

forward declaration does not need to be at the top of the file as follows

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

is the same as

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

the recommended friend syntax just uses the later

Sissie answered 27/3, 2012 at 13:56 Comment(1)
For friend declarations it is not recommended but rather required in C++03, and has a slightly different meaning in C++11 (where and friend class X cannot be used to refer to template parameters)Pat

© 2022 - 2024 — McMap. All rights reserved.