如何前来申报不需要朋友类的概念? [英] How come forward declaration is not needed for friend class concept?

查看:222
本文介绍了如何前来申报不需要朋友类的概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近刚刚学习了C ++中的 friend class 概念(我已经google了一下,但这回答让我笑了,直到我记得最重要的部分),我正试图将它并入我正在工作的项目。简单的问题最后被挑出来,但一般来说,我对工作代码中的完全缺乏前进声明感到困惑。



我的所有类通过(子)文件夹分隔,并且每个类都分成一个单独的.h 和 .cpp 文件,但这应该足以让你感觉到依赖性:

  // FE.h  - 无实现 - 无.cpp文件
类FE
{
private:
virtual void somePrivateFunc()= 0;
// 90%虚拟类,用于进一步实现的接口
friend类TLS;
};

// DummyFE.h
#includeFE.h
class DummyFE :: public FE {
/ * singleton dummy * /
private :
// constructor
public:
static DummyFE& instance();
};
// DummyFE.cpp
#includeDummyFE.h
//所有Dummy FE实现

// ImplFE.h
#include FE.h
class ImplFE :: public FE {/ * implemented * /};
// ImplFE.cpp
#includeFE.cpp
//所有Impl FE实现


// SD.h - 实现策略设计模式
//(真正的项目在这里不仅仅是FE类)
#includeFE.h
#includeDummyFE.h
class SD
{
private:
FE& localFE;
public:
SD(FE& paramFE = DummyFE :: instance());
// ...和所有其他phun东西...
friend class TLS;
};
// SD.cpp - 实现
#包含SD.h
/ * SD实现* /

// TLS.h - 实现策略设计模式
(更高级别)
#include SD.h
class TLS {
private:
SD * subStrategy;
public:
void someFunctionRequiringFriendliness();
}

// TLS.cpp - 实现
#includeTLS.h
void TLS :: someFunctionRequiringFriendliness(){
this-> ; subStrategy-> localFE.somePrivateFunc(); // 好!
}

现在,我已经派对了所有这些,依赖(不得不写到一个类图中到底使它工作),但现在它。实际上使我感到困惑的事实 不需要向前声明。我知道之前的声明,为了防止我用这个答案刷新我的记忆。



因此,为了尝试清楚,我的问题
当声明 class TLS 作为朋友,怎么不需要明确的前瞻性声明?这是否意味着朋友类声明是一个向前声明的所有在它自己?对我来说,直观地,这里的东西丢失了...而且由于它编译和工作正常,有人可以帮助纠正我的直觉? :D



PS对于这个问题和一堆代码的这么长的介绍很抱歉。请不要评论我的代码概念 - 朋友在这里很好,我很确定它是正确的我当前的项目(这只是有点难以从这个骨架看到)。

解决方案

你说得对,朋友的声明是正确的



以下编译:

 一个; 
class B
{
friend A;
};

  class B 
{
friend class A;
};

这不会:

  class B 
{
friend A;
};
声明前缀声明 A类,但是关键字。这就是为什么第二个例子不工作,因为它不知道 A 是什么。如果你事先声明了 A ,就像在第一个片段中一样,它可以解析类声明的 A



我已改正。 a>


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.

解决方案

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.

这篇关于如何前来申报不需要朋友类的概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆