友元

main.cpp

#include <iostream>
using namespace std;
class Me{
private:
    int id;
    int money;
    friend void test(void);
    friend class MyBestFriend;
};
class MyBestFriend{
public:
    MyBestFriend(){
        Me m;
        m.money = 100;
        cout << m.money << endl;
    }
};
void test(void){
    Me m;
    m.id = 3;
    cout << m.id << endl;
}
int main(){
    test();
    MyBestFriend mbf;
    cout << "letben" << endl;
    system("pause");
    return 0;
}

运行结果:

原文地址:https://www.cnblogs.com/letben/p/5295141.html