简体   繁体   English

指定一个class成员function为另一个class的好友?

[英]Specify a class member function as a friend of another class?

According to the C++ Primer book, the author mentioned that we can specify a class member function as a friend of another class, instead of the entire class (page 634).根据 C++ Primer 书,作者提到我们可以指定一个 class 成员 function 作为另一个 class 的朋友,而不是整个 class(第 634 页)。

Then, I tested this code:然后,我测试了这段代码:

class A
{
public:
    friend void B::fB(A& a);
    void fA(){}
};
class B
{
public:
    void fB(A& a){};
    void fB2(A& a){};
};

I just wanted the fB() to be friend of class A , not the entire class B .我只是想让fB()成为class A朋友,而不是整个class B的朋友。 But the above code produced an error: 'B': is not a class or namespace name .但是上面的代码产生了一个错误: 'B': is not a class or namespace name (I am using Visual C++ 2005) (我使用的是 Visual C++ 2005)

Try putting the B definition before A's:尝试将 B 定义放在 A 之前:

class A; // forward declaration of A needed by B

class B
{
public:
    // if these require full definition of A, then put body in implementation file
    void fB(A& a); // Note: no body, unlike original.
    void fB2(A& a); // no body.
};

class A
{
public:
    friend void B::fB(A& a);
    void fA(){}
};

A needs the full definition of B . A需要B的完整定义。 However, B needs to know about A , but does not need the full definition, so you need the forward declaration of A .但是, B需要知道A ,但不需要完整定义,因此您需要A的前向声明。

When the compiler starts compiling the code( usually from top ) and it encounters this line:当编译器开始编译代码(通常从 top 开始)并遇到以下行时:

friend void B::fB(A& a);
  1. at this point, compiler has no idea about type info of B so it throws an error ( 'B' : is not a class or namespace name ).此时,编译器不知道 B 的类型信息,因此它会引发错误( 'B' : is not a class or namespace name )。
  2. by forward declaration of class B, compiler knows about type of B is Class in advance to its actual declaration with all members.通过类 B 的前向声明,编译器在其所有成员的实际声明之前就知道 B 的类型是 Class。

  3. run below code after forward declaration of class B.在 B 类前向声明​​后运行下面的代码。

/////////////// ///////////////

class B;
class A
{
public:
    friend void B::fB(A& a); 
    void fA(){};
};
class B
{
public:
    void fB(A& a){};
    void fB2(A& a){};
};

Still error !!!还是报错!!!

because forward declaration is just a declaration of an identifier for which the programmer has not yet given a complete definition.因为前向声明只是一个标识符的声明,程序员还没有给出完整的定义。 so compiler needs full definition of B before class A.所以编译器需要在类 A 之前完整定义 B。

Note: class A definition dependents on type of B and also definition of B (ie B::fB) so that forward declaration alone can not resolve, complete definition of class B needs to define before class A.注意:A 类定义依赖于 B 的类型以及 B 的定义(即 B::fB),因此仅靠前向声明无法解析,B 类的完整定义需要在 A 类之前定义。

4 run this code 4 运行这段代码

//////// ////////

class B
{
public:
    void fB(A& a){};
    void fB2(A& a){};
};
class A
{
public:
    friend void B::fB(A& a); 
    void fA(){}
};

Still error !!!还是报错!!!

because class B member functions fB & fB2 having arguments of type A but compiler has no idea about type info of A so by forward declaration of class A, we can let compiler knows about type info of A. Note: class B definition only dependent on type of A not the members of A so that forward declaration of A resolve step 4.因为 B 类成员函数 fB & fB2 具有类型 A 的参数,但编译器不知道 A 的类型信息,所以通过类 A 的前向声明,我们可以让编译器知道 A 的类型信息。 注意:B 类定义仅依赖于A 的类型不是 A 的成员,以便 A 的前向声明解决步骤 4。

  1. final code最终代码

//////////////////////// ////////////////////////

class A;  // forward declaration of A needed by B
class B
{
public:
    void fB(A& a);
};

class A
{
    int i;
public:
    friend void fA(A& a);    //specifying function fA as a friend of A, fA is not member function of A
    friend void B::fB(A& a); //specifying B class member function fB as a friend of A
};

// fA is Friend function of A
void fA(A& a)
{
    a.i  = 11; // accessing and modifying Class A private member i
    cout<<a.i<<endl;
}

// B::fB should be defined after class A definition only because this member function can access Class A members
void B::fB(A& a)
{
    a.i  = 22; // accessing and modifying Class A private member i in Class B member function fB
    cout<<a.i<<endl;
}

int main()
{
    A a;
    fA(a);    // calling friend function of class A

    B b;
    b.fB(a);  // calling B class member function fB, B:fB is friend of class A

    return 0;
}

6 Exercise: 6 练习:

// Cyclic dependency 
#include<iostream>
using namespace std;

class A;

class B
{
public:
    void fB(A& a);
    friend void A::fA(B& b); //specifying class A's member function fA as a friend of B
};

class A
{
    int i;
public:
    void fA(B& b);  
    friend void B::fB(A& a); //specifying class B's member function fB as a friend of A
};

int main()
{
    return 0;
}

For this to work, the full definition of B needs to be known before the definition of A .为此,需要在A的定义之前知道B的完整定义。

So forward declare A , since B doesn't need the full type, and switch the definitions around:所以向前声明A ,因为B不需要完整类型,并切换定义:

class A;
class B
{
public:
    void fB(A& a){};
    void fB2(A& a){};
};
class A
{
public:
    friend void B::fB(A& a);
    void fA(){}
};

When the compiler starts reading the code( usually from top ) and it encounters this line:当编译器开始读取代码(通常从 top )并遇到以下行时:

friend void B::fB(A& a);

Then compiler doesn't understand what do you mean by this B:: .然后编译器不明白这个B::是什么意思。 Even if you have defined this class later in the code but compiler doesn't know that.即使您稍后在代码中定义了这个类,但编译器不知道。 So it's usually a good to practice to do forward declaration of class( class Name; ) if the definition resides later in the code.因此,如果定义位于代码的后面,那么练习对 class( class Name; ) 进行前向声明通常是一个很好的做法。

firstly forward declare class A, so that its visible in class B definition.首先转发声明A类,使其在B类定义中可见。 then define class A containing a friend function from class B.然后定义包含来自类 B 的友元函数的类 A。

First of all before using a particular class name you will have to declare it first.首先,在使用特定的类名之前,您必须先声明它。 So you will need a forward declaration of the Class B as you are using it in Class A before the Class B has been originally declared.因此,在最初声明 B 类之前,您需要在 A 类中使用 B 类的前向声明。

Secondly you will need to define the functions (that are using variables from both the classes- here the friend functions ) after both the classes have been defined.其次,在定义了两个类之后,您需要定义函数(使用来自两个类的变量——这里是友元函数)。 Or else we may face errors.否则我们可能会遇到错误。

For example例如

#include<iostream>

using namespace std;

class alpha1;

class alpha2
{
    public:

        void put_bata(int a,int b);
};

void alpha2 :: put_bata(int a,int b)
{
    alpha1 net;

    net.roll=a;

    net.id=b;

    net.get_data();
}

class alpha1
{
    int roll;

    int id;

    public:

        void get_data(void)
        {
            cout<<roll<<endl<<id<<endl;
        }

        friend void alpha2 :: put_bata(int a,int b);
};

int main()
{
    alpha2 gamma;

    gamma.put_bata(5,6);

    return 0;
}

Will show us errors as put_bata tries to access roll and id before they are defined even if we had a forward declaration of the class but the code given below will just work fine.将向我们显示错误,因为 put_bata 尝试在定义之前访问 roll 和 id,即使我们有类的前向声明,但下面给出的代码将正常工作。

#include<iostream>

using namespace std;

class alpha1;

class alpha2
{
    public:

        void put_bata(int a,int b);
};

class alpha1
{
    int roll;

    int id;

    public:

        void get_data(void)
        {
            cout<<roll<<endl<<id<<endl;
        }

        friend void alpha2 :: put_bata(int a,int b);
};

void alpha2 :: put_bata(int a,int b)
{
    alpha1 net;

    net.roll=a;

    net.id=b;

    net.get_data();
}


int main()
{
    alpha2 gamma;

    gamma.put_bata(5,6);

    return 0;
}

@juanchopanza @ipkiss Regarding the problem that you can't access data members of A inside fB(A& a) because A is not defined yet. @juanchopanza @ipkiss 关于由于 A 尚未定义而无法访问 fB(A& a) 内的 A 数据成员的问题。 Instead of defining it in a separate file and including it, you can just define the function fB(A& a) after the definition of class A so that fB(A& a) is able to see data members of A.无需在单独的文件中定义并包含它,您只需在类 A 的定义之后定义函数 fB(A& a),以便 fB(A& a) 能够看到 A 的数据成员。

#include <iostream>
#include <string>
using namespace std;
 
class Manager;
class Employee;
 
class Admin{
    public:
    void EmployeeDetails(Employee &obj);
    void ManagerDetails(Manager &obj);
};
 
class Employee{
    string name = "praful";
    friend void Admin::EmployeeDetails(Employee &obj);    
};
 
class Manager{
    string name = "Robert";
    friend void Admin::ManagerDetails(Manager &obj);
};
 
 
void Admin::EmployeeDetails(Employee &obj)
{
    cout << "Employee :" << obj.name << endl;
}
void Admin::ManagerDetails(Manager &obj)
{
    cout << "Manager :" << obj.name << endl;        
}
 
int main()
{
    Employee e;
    Manager m;
    Admin a;
    a.EmployeeDetails(e);
    a.ManagerDetails(m);
    return 0;
}

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

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