📜  C++中的Friend类和函数

📅  最后修改于: 2021-05-30 13:58:14             🧑  作者: Mango

朋友类别朋友类别可以访问被声明为朋友的其他类别的私有和受保护成员。有时允许特定的类访问其他类的私有成员很有用。例如,可以允许LinkedList类访问Node的私有成员。

CPP
class Node {
private:
    int key;
    Node* next;
    /* Other members of Node Class */
 
    // Now class  LinkedList can
    // access private members of Node
    friend class LinkedList;
};


CPP
class Node {
private:
    int key;
    Node* next;
 
    /* Other members of Node Class */
    friend int LinkedList::search();
    // Only search() of linkedList
    // can access internal members
};


CPP
#include 
class A {
private:
    int a;
 
public:
    A() { a = 0; }
    friend class B; // Friend Class
};
 
class B {
private:
    int b;
 
public:
    void showA(A& x)
    {
        // Since B is friend of A, it can access
        // private members of A
        std::cout << "A::a=" << x.a;
    }
};
 
int main()
{
    A a;
    B b;
    b.showA(a);
    return 0;
}


CPP
#include 
 
class B;
 
class A {
public:
    void showB(B&);
};
 
class B {
private:
    int b;
 
public:
    B() { b = 0; }
    friend void A::showB(B& x); // Friend function
};
 
void A::showB(B& x)
{
    // Since showB() is friend of B, it can
    // access private members of B
    std::cout << "B::b = " << x.b;
}
 
int main()
{
    A a;
    B x;
    a.showB(x);
    return 0;
}


CPP
#include 
 
class A {
    int a;
 
public:
    A() { a = 0; }
 
    // global friend function
    friend void showA(A&);
};
 
void showA(A& x)
{
    // Since showA() is a friend, it can access
    // private members of A
    std::cout << "A::a=" << x.a;
}
 
int main()
{
    A a;
    showA(a);
    return 0;
}


朋友函数像朋友类一样,可以对朋友函数给予特殊的授予,以访问私有成员和受保护成员。朋友函数可以是:
a)其他班级的成员
b)全局函数

CPP

class Node {
private:
    int key;
    Node* next;
 
    /* Other members of Node Class */
    friend int LinkedList::search();
    // Only search() of linkedList
    // can access internal members
};

以下是有关朋友函数和类的一些重要点:
1)朋友只能用于有限的目的。太多的函数或外部类被声明为具有受保护或私有数据的类的朋友,这降低了在面向对象编程中封装单独类的价值。
2)友谊不是相互的。如果A类是B的朋友,那么B不会自动成为A的朋友。
3)友谊不是继承的(有关更多详细信息,请参见此内容)
4)朋友的概念在Java不存在。
一个简单而完整的C++程序来演示好友类

CPP

#include 
class A {
private:
    int a;
 
public:
    A() { a = 0; }
    friend class B; // Friend Class
};
 
class B {
private:
    int b;
 
public:
    void showA(A& x)
    {
        // Since B is friend of A, it can access
        // private members of A
        std::cout << "A::a=" << x.a;
    }
};
 
int main()
{
    A a;
    B b;
    b.showA(a);
    return 0;
}

输出:

A::a=0

一个简单而完整的C++程序,用于演示另一个类的好友函数

CPP

#include 
 
class B;
 
class A {
public:
    void showB(B&);
};
 
class B {
private:
    int b;
 
public:
    B() { b = 0; }
    friend void A::showB(B& x); // Friend function
};
 
void A::showB(B& x)
{
    // Since showB() is friend of B, it can
    // access private members of B
    std::cout << "B::b = " << x.b;
}
 
int main()
{
    A a;
    B x;
    a.showB(x);
    return 0;
}

输出:

B::b = 0

一个简单而完整的C++程序,向全球朋友展示

CPP

#include 
 
class A {
    int a;
 
public:
    A() { a = 0; }
 
    // global friend function
    friend void showA(A&);
};
 
void showA(A& x)
{
    // Since showA() is a friend, it can access
    // private members of A
    std::cout << "A::a=" << x.a;
}
 
int main()
{
    A a;
    showA(a);
    return 0;
}

输出:

A::a = 0
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”