-
友
元
类
\color{green}{友元类}
友元类
B是A的友元类则B可以访问A的任何成员。
如下
在TV.h中声明TV有友元类Tele#pragma once class TV { public: friend class Tele; private: static int a; };
Tele.h如下:
#pragma once #include "TV.h" class Tele { public: int add() {}; private: int b; };
在Tele.cpp中可以对a 进行调用
#include "Tele.h" #include <iostream> using namespace std; int Tele::add() { return TV::a + b; }