目录
友元函数:
友元类,和友元函数,使用friend关键字进行声明即可,与访问权限无关,
所以,可以放在private/pulic/protected任意区域内。
使用全局函数作为友元函数:
Computer.h
#pragma once
#include <string>
class Computer
{
public:
Computer();
// 使用全局函数作为友元函数
friend void upgrade(Computer* computer);
std::string description();
private:
std::string cpu; //CPU芯片
};
Computer.cpp
#include "Computer.h"
#include <sstream>
Computer::Computer()
{
cpu = "i7";
}
std::string Computer::description()
{
std::stringstream ret;
ret << "CPU:" << cpu;
return ret.str();
}
main.cpp
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include "Computer.h"
void upgrade(Computer* computer) {
computer->cpu = "i9"; //直接访问对象的私有数据成员!!!
}
int main(void) {
Computer shanxing;
std::cout << shanxing.description() << std::endl;
upgrade(&shanxing);
std::cout << shanxing.description() << std::endl;
system("pause");
return 0;
}
使用类的成员函数作为友元函数:
Computer.h
#pragma once
#include <string>
// class ComputerService;
// 仅仅声明ComputerService不够,需要包含头文件
#include "ComputerService.h"
class Computer
{
public:
Computer();
// 使用全局函数作为友元函数
friend void upgrade(Computer* computer);
// 使用类的成员函数,作为友元函数
friend void ComputerService::upgrade(Computer* comptuer);
std::string description();
private:
std::string cpu; //CPU芯片
};
Computer.cpp
#include "Computer.h"
#include <sstream>
Computer::Computer()
{
cpu = "i7";
}
std::string Computer::description()
{
std::stringstream ret;
ret << "CPU:" << cpu;
return ret.str();
}
ComputerService.h
#pragma once
class Computer;
class ComputerService
{
public:
void upgrade(Computer* computer);
};
ComputerService.cpp
#include "ComputerService.h"
#include "Computer.h"
void ComputerService::upgrade(Computer* computer) {
computer->cpu = "i9";
}
main.cpp
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include "Computer.h"
#include "ComputerService.h"
int main(void) {
Computer shanxing;
ComputerService service;
std::cout << shanxing.description() << std::endl;
service.upgrade(&shanxing);
std::cout << shanxing.description() << std::endl;
system("pause");
return 0;
}
功能上,这两种形式,都是相同,应用场合不同。
一个是,使用普通的全局函数,作为自己的朋友,实现特殊功能。
一个是,使用其他类的成员函数,作为自己的朋友,实现特殊功能。
友元类:
一个类 A 可以将另一个类 B 声明为自己的友元,类 B 的所有成员函数就都可以访问类 A 对象的私有成员。
Computer.h
#pragma once
#include <string>
class ComputerService;
class Computer
{
public:
Computer();
std::string description();
private:
std::string cpu; //CPU芯片
// 友元类
friend class ComputerService;
};
Computer.cpp
#include "Computer.h"
#include <sstream>
Computer::Computer()
{
cpu = "i7";
}
std::string Computer::description()
{
std::stringstream ret;
ret << "CPU:" << cpu;
return ret.str();
}
ComputerService.h
#pragma once
class Computer;
class ComputerService
{
public:
void upgrade(Computer* computer);
void clean(Computer* computer); //计算机清理
void kill(Computer* computer); //杀毒
};
ComputerService.cpp
#include "ComputerService.h"
#include "Computer.h"
#include <iostream>
void ComputerService::upgrade(Computer* computer) {
computer->cpu = "i9";
}
void ComputerService::clean(Computer* computer)
{
std::cout << "正在对电脑执行清理[CPU:"
<< computer->cpu << "]..."
<< std::endl;
}
void ComputerService::kill(Computer* computer)
{
std::cout << "正在对电脑执行杀毒[CPU:"
<< computer->cpu << "]..."
<< std::endl;
}
main.cpp
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include "Computer.h"
#include "ComputerService.h"
int main(void) {
Computer shanxing;
ComputerService service;
std::cout << shanxing.description() << std::endl;
service.upgrade(&shanxing);
service.clean(&shanxing);
service.kill(&shanxing);
std::cout << shanxing.description() << std::endl;
system("pause");
return 0;
}
其他友元类:
main.cpp
#include<iostream>
using namespace std;
class B;
class A
{
public:
A() :x(520) {};
// 类B作为类A的友元类, 如果类A中的一个函数的函数特征包含类B, 那么这个函数作为友元全局函数,
// 只能访问B累的公有成员, 但能访问A类的所有成员
friend void data(A& a, B& b);
// 既要访问类A的私有数据成员也要访问类B的私有数据成员, 那么这个函数在两个类里面都声明为友元成员函数
friend void common(A& a, B& b);
private:
int x;
friend class B;
};
class B
{
public:
B() :y(888) {};
friend void common(A& a, B& b);
private:
int y;
};
void common(A& a, B& b)
{
cout << "a = " << a.x << endl;
cout << "y = " << b.y << endl;
}
void data(A& a, B& b)
{
cout << "a = " << a.x << endl;
//cout << "y = " << b.y << endl;
}
int main(void)
{
A a;
B b;
data(a, b);
common(a, b);
system("pause");
return 0;
}