简体   繁体   English

为什么我不能在一个属于另一个类的私有成员的类中声明一个朋友?

[英]Why can't I declare a friend in one class that is a private member of another class?

Given the following code: 给出以下代码:

class Screen;

class WindowMgr
{
    WindowMgr& relocateScreen( int r, int c, Screen& s);
};

class Screen
{
    friend WindowMgr& WindowMgr::relocateScreen( int r, int c, Screen& s);
    // ^ cannot access private member declared in class 'WindowMgr'

    int m_nR,
        m_nC;
};

WindowMgr& WindowMgr::relocateScreen( int r, int c, Screen& s)
{
    s.m_nR = r;
    s.m_nC = c;
    return *this;
}

Why can the Screen class not declare the WindowMgr::relocateScreen member function as a friend? 为什么Screen类不能将WindowMgr::relocateScreen成员函数声明为朋友? Screen is not wanting to use this private member-function of another class, but simply wants that function to be able access its own private members. Screen不想使用另一个类的私有成员函数,而只是希望该函数能够访问自己的私有成员。

Making the relocateScreen function public might be bad design if it's intented only for use within the WindowMgr class. 使得relocateScreen ,如果是只用在内部使用功能的公共可能是糟糕的设计WindowMgr类。 Equally, making Screen a friend of WindowMgr might be bad design if it is not intented to access private members of WindowMgr in any other case. 同样,如果WindowMgr在任何其他情况下访问WindowMgr私有成员, WindowMgr使Screen成为WindowMgr的朋友可能是糟糕的设计。

Where am I going wrong here? 我在哪里错了? What is the correct approach? 什么是正确的方法? Am I making a fool of myself? 我自欺欺人了吗?

The friend declaration doesn't work because WindowMgr::relocateScreen() is private to WindowMgr . 友元声明不起作用,因为WindowMgr::relocateScreen()WindowMgr

C++ standard 11.4-7: C ++标准11.4-7:

"A name nominated by a friend declaration shall be accessible in the scope of the class containing the friend declaration..." “朋友声明提名的名称应在包含朋友声明的类别范围内可访问......”

Personally, I would make relocateScreen() a private member function of Screen and make WindowMgr a friend of Screen . 就个人而言,我会将WindowMgr relocateScreen()作为Screen的私有成员函数,并使WindowMgr成为Screenfriend That way, WindowMgr can just call relocateScreen() on Screen and won't have to touch any of the data members of Screen . 这样, WindowMgr只能在Screen上调用WindowMgr relocateScreen() ,而不必触及Screen任何数据成员。

The WindowMgr will have to declare Screen as a friend. WindowMgr必须将Screen声明为朋友。 You can use a forward declaration. 您可以使用前向声明。

Why not factor the WindowMgr::relocateScreen out into a different class, say WindowMgrFoo witht the 1 relocateScreen function. 为什么不将WindowMgr :: relocateScreen分解为另一个类,比如说WindowMgrFoo和1 relocateScreen函数。 Delcare WindowMgrFoo a friend of Screen in Screen and have WindowMgr inherit privately from WindowMgrFoo? Delcare WindowMgrFoo是Screen in Screen的朋友,WindowMgr是否从WindowMgrFoo私下继承? Or just have WindowMgr have a reference to a WindowMgrFoo, but that you need to change how it's called by users if you make it public. 或者只是让WindowMgr具有对WindowMgrFoo的引用,但是如果将其公开,则需要更改用户调用它的方式。

In Silico - Nice one for citing the standard. 在Silico - 很好的引用标准。 Having slept on it I now see the rationale: 睡了之后我现在看到理由:

By declaring WindowMgr::relocateScreen with its paramater list to be a friend in Screen , the Screen class becomes dependent on the private implementation of the WindowMgr class. 通过声明WindowMgr::relocateScreen及其参数列表成为Screen的朋友, Screen类变得依赖于WindowMgr类的私有实现。 This violates encapsulation/information hiding. 这违反了封装/信息隐藏。

In order to not violate the tenets of OOD, only public member-functions of class can be declared as friends in another, because otherwise the latter becomes dependent on the private implementation of the former. 为了不违反OOD的原则,只有类的公共成员函数可以在另一个中声明为朋友,否则后者变得依赖于前者的私有实现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么PRIVATE成员函数不能成为另一个类的朋友函数? - Why can't a PRIVATE member function be a friend function of another class? 为什么我不能将此成员函数作为另一个类的朋友? - Why can't I make this member function friend of another class? 为什么这个朋友的功能无法访问该类的私有成员? - Why this friend function can't access a private member of the class? 为什么我的朋友类不能访问私有成员? - Why can't my friend class access a private member? 为什么我不能从不同命名空间中的友元类更改类的私有成员? - Why can't I change a private member of a class from a friend class in a different namespace? 为什么我不能转发声明内部朋友课程? - Why can't I forward declare an internal friend class? 为什么朋友 class 成员可以通过公共继承的 class 的 object 访问其成为朋友的 class 的私人成员? - Why a friend class member can access a private member of the class it is being friend to through an object of publicly inherited class? 无法使用朋友类更改类的私有成员的值 - Can't change the value of private member of a class using friend class 为什么我不能在带有私有构造函数的c ++类中声明一个空的构造函数 - Why can't I declare an empty constructor in a c++ class that extends from one with a private constructor 为什么我不能在友元类中实例化其构造函数是私有的类? - Why can I not instantiate a class whose constructor is private in a friend class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM