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; }