C++>friend

  • \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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章