#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
//1.友元关系不能被继承
//2.友元关系是单向的,不具备交换性 类B是A的友元,反之未必
//3.友元关系不具备传递性 B是A的友元 C是B的友元 但C不一定是A的友元
class A{
public:
A(int a){
this->m_a=a;
}
void printA(){
cout<<"m_a: "<<m_a<<endl;
}
friend class B;//声明友元类
private:
int m_a;
};
class B{
public:
B(int b){
this->m_b=b;
}
void printB(){
A a(1000);
cout<<"m_a: "<<a.m_a<<endl;
cout<<"m_b: "<<m_b<<endl;
}
private:
int m_b;
};
int main(){
B b(10);
b.printB();
system("pause");
return 0;
}
友元类及友元关系
最新推荐文章于 2024-06-20 09:00:44 发布