简体   繁体   English

好友功能和受保护的数据

[英]Friend function and protected data

I have class 我有课

    class ScoreBoard: public die
{ //..//
    protected:
    bool mSetValue[6];
    public:
//...//
        friend void ValueSet();
};

and I would like to use that friendship to get access to mSetValue . 我想利用这种友谊来访问mSetValue So what I do in ScoreBoard.cpp is: I predifine a funcion ( void ValueSet(); ) and then define it like this: 因此,我在ScoreBoard.cpp是:我void ValueSet();定义了一个void ValueSet(); ),然后按以下方式定义它:

void ValueSet()
{ char lPick;
std::cin >> lPick;
if (lPick == 1) mSetValue[0] = true; }

But the debugger says: 但是调试器说:

'mSetValue' was not declared in this scope. 未在此范围内声明“ mSetValue”。

So, my question is - how to properly set a friendship so ValueSet can get access to mSetValue array? 因此,我的问题是-如何正确设置友谊,以便ValueSet可以访问mSetValue数组?

Member variables exist inside instances (objects). 成员变量存在于实例(对象)内部。 They are also called "instance variables". 它们也称为“实例变量”。 If there is no instance of the class, then there is no instance variable. 如果没有该类的实例,则没有实例变量。

mSetValue is a member variable of ScoreBoard . mSetValueScoreBoard的成员变量。 Therefore ScoreBoard::mSetValue instances exists only within an instance of ScoreBoard . 因此, ScoreBoard::mSetValue实例仅存在于ScoreBoard实例中。

how to properly set a friendship so ValueSet can get access to mSetValue array? 如何正确设置友谊,以便ValueSet可以访问mSetValue数组?

In function void ValueSet() you don't have any instances of ScoreBoard . void ValueSet()函数中,您没有ScoreBoard任何实例。 You cannot access ScoreBoard::mSetValue - regardless of its access specifier or friendship - because it doesn't exist. 您不能访问ScoreBoard::mSetValue不管其访问说明符或友好关系-因为它不存在。 What you need is an instance of ScoreBoard . 您需要的是ScoreBoard一个实例。

I use this function inside the ScoreBoard class so I can't create any instance of ScoreBoard inside it 我在ScoreBoard类中使用了此函数,因此无法在其中创建ScoreBoard的任何实例

Nothing prevents you from creating an instance of ScoreBoard within a member function of ScoreBoard . 没有什么能阻止你从创建的实例ScoreBoard的成员函数内的ScoreBoard Although, within a member function of ScoreBoard you already have access to an instance, which is pointed by this , so there might be no need to create a new instance. 尽管在ScoreBoard的成员函数中您已经可以访问实例(由this指向),所以可能不需要创建新实例。 What you should do depends on your intention. 您应该做什么取决于您的意图。

Given your comment, I suspect that a member function would be more appropriate for you, rather than a free function. 根据您的评论,我怀疑成员函数比自由函数更适合您。

I use the generic term "Member variable" to refer non-static member variables for simplicity. 为了简化起见,我使用通用术语“成员变量”来指代非静态成员变量。 Static member variables are different. 静态成员变量是不同的。 They are also called class variables. 它们也称为类变量。

Friend functions are not member of class. 朋友功能不是该类的成员。 So in your case if you want to use mSetValue in ValueSet then you have to provide some access to mSetValue via an instance or "object" of that class. 所以你的情况,如果你想使用mSetValueValueSet那么你必须提供一些访问mSetValue通过一个实例类或“对象”。 This can be done by declaring your ValueSet() function as 这可以通过将ValueSet()函数声明为

friend void ValueSet(ScoreBoard &sb);

And your definition as 和你的定义为

void ValueSet(ScoreBoard &sb)
{
    char lPick;
    std::cin >> lPick;
    if (lPick == 1)
        sb.mSetValue[0] = true; 
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM