如何排序类和友元函数的定义和声明?

发布于 2025-01-12 11:21:23 字数 512 浏览 4 评论 0原文

我有一个函数是类的友元。因此该函数可以访问所有类成员。 但是,我对定义的顺序感到困惑。

如果函数位于类之前,那么它如何才能获取类成员呢?如果类位于函数之前,则它无法知道该函数。

如何解决这种交叉引用冲突?

所以我的代码

class BP_Interface
{
   friend void CommonUtility::HandleToFrame(Handle&);
private:
   Handle frame_in_model;
}

// followed by definition
namespace CommonUtility
{
    void HandleToFrame(Handle& eR)
    {
       BP_Interface().frame_in_model = eR;
       return;
    }
}


Visual Studio IDE 显示错误,并表示无法访问 frame_in_model

I have a function that is friend of a class. Therefore the function can access all class member.
However, I am confused with the order of definition.

If the function comes before the class then how do could it even get the class member? if the class come before the function, then it cannot know the function.

How to solve such cross reference conflict?

So I have code

class BP_Interface
{
   friend void CommonUtility::HandleToFrame(Handle&);
private:
   Handle frame_in_model;
}

// followed by definition
namespace CommonUtility
{
    void HandleToFrame(Handle& eR)
    {
       BP_Interface().frame_in_model = eR;
       return;
    }
}


visual studio IDE shows error, and says frame_in_model cannot be accessed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜还是长夜 2025-01-19 11:21:23

在定义 BP_Interface 之前声明 CommonUtility::HandleToGraph 并在稍后定义它。

namespace CommonUtility
{
    void HandleToGraph(Handle& eR);
}

class BP_Interface
{
   friend void CommonUtility::HandleToFrame(Handle&);
private:
   Handle frame_in_model;
};

// followed by definition
namespace CommonUtility
{
    void HandleToGraph(Handle& eR)
    {
       BP_Interface().frame_in_model = eR;
       return;
    }
}

Declare CommonUtility::HandleToGraph before the definition of BP_Interface and define it later.

namespace CommonUtility
{
    void HandleToGraph(Handle& eR);
}

class BP_Interface
{
   friend void CommonUtility::HandleToFrame(Handle&);
private:
   Handle frame_in_model;
};

// followed by definition
namespace CommonUtility
{
    void HandleToGraph(Handle& eR)
    {
       BP_Interface().frame_in_model = eR;
       return;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文