不可或缺 Windows Native (20) - C++: 友元函数, 友元类
作者:webabcd
介绍
不可或缺 Windows Native 之 C++
- 友元函数
- 友元类
示例
演示友元函数, 友元类
CppClass4.h
#pragma once #include <string> using namespace std; namespace NativeDll
{
class CppClass4
{
public:
string Demo();
};
}
CppClass4.cpp
/*
* 友元(friend)函数, 友元类
*
* 友元函数: C++ 增加了一种称之为“友元(friend)”函数的声明,其用于将“特权”在本类中赋给一些函数(可以是全局函数,也可以是其它类的成员函数),使之能够访问本类的私有和保护成员
* 友元类: 如果 B 类是 A 类的友元类,则 B 中的所有函数都是 A 的友元函数,即 B 可以访问 A 的所有私有和保护成员
*/ #include "pch.h"
#include "CppClass4.h"
#include "cppHelper.h" using namespace NativeDll; void cppclass4_demo1();
void cppclass4_demo2();
void cppclass4_demo3(); string CppClass4::Demo()
{
// 全局函数作友元函数
cppclass4_demo1(); // 其它类的成员函数作友元函数
cppclass4_demo2(); // 友元类
cppclass4_demo3(); return "看代码及注释吧";
} class CppClass4Demo1Time
{
public:
CppClass4Demo1Time(int h, int m, int s)
{
_hour = h;
_minute = m;
_second = s;
} // 友元函数声明:声明全局函数 string ShowTime(CppClass4Demo1Time &); 是我的友元函数
friend string ShowTime(CppClass4Demo1Time &); // 前面加 friend private:
int _hour;
int _minute;
int _second;
}; // 之前声明过的,我是 CppClass4Demo1Time 类的友元函数,我可以使用 CppClass4Demo1Time 对象中的 private 和 protected 属性和函数
string ShowTime(CppClass4Demo1Time &t)
{
t._hour = ;
return int2string(t._hour) + ":" + int2string(t._minute) + ":" + int2string(t._second);
} // 全局函数作友元函数的演示
void cppclass4_demo1()
{
CppClass4Demo1Time t(, , );
string result = ShowTime(t); // 10:30:15
} class CppClass4Demo2Date; // 对 CppClass4Demo2Date 类的提前引用声明
class CppClass4Demo2Time
{
public:
CppClass4Demo2Time(int h, int m, int s)
{
_hour = h;
_minute = m;
_second = s;
} // 我这里用到了 CppClass4Demo2Date 类,但是其是在后面定义的,所以在此之前要通过“class CppClass4Demo2Date;”做提前引用声明
string ShowTime(CppClass4Demo2Date &d); private:
int _hour;
int _minute;
int _second;
}; class CppClass4Demo2Date
{
public:
CppClass4Demo2Date(int y, int m, int d)
{
_year = y;
_month = m;
_day = d;
} // 友元函数声明:声明 CppClass4Demo2Time 类中的 string ShowTime(CppClass4Demo2Date &); 是我的友元函数
friend string CppClass4Demo2Time::ShowTime(CppClass4Demo2Date &); private:
int _year;
int _month;
int _day; }; // 之前声明过的,我是 CppClass4Demo2Date 类的友元函数,我可以使用 CppClass4Demo2Date 对象中的 private 和 protected 属性和函数
string CppClass4Demo2Time::ShowTime(CppClass4Demo2Date &d)
{
d._year = ;
return int2string(d._year) + "-" + int2string(d._month) + "-" + int2string(d._day) + " " + int2string(_hour) + ":" + int2string(_minute) + ":" + int2string(_second);
} // 其它类的成员函数作友元函数的演示
void cppclass4_demo2()
{
CppClass4Demo2Time t(, , );
CppClass4Demo2Date d(, , );
string result = t.ShowTime(d); // 2016-4-3 8:30:15
} class CppClass4Demo3Date;
class CppClass4Demo3Time
{
public:
CppClass4Demo3Time(int h, int m, int s)
{
_hour = h;
_minute = m;
_second = s;
} string ShowTime(CppClass4Demo3Date &d); private:
int _hour;
int _minute;
int _second;
}; class CppClass4Demo3Date
{
// 友元类声明:声明 CppClass4Demo3Time 类是我的友元类(CppClass4Demo3Time 类中的成员函数可以访问我的 private 和 protected 属性和函数)
friend CppClass4Demo3Time; public:
CppClass4Demo3Date(int y, int m, int d)
{
_year = y;
_month = m;
_day = d;
} private:
int _year;
int _month;
int _day;
}; // CppClass4Demo3Time 是 CppClass4Demo3Date 的友元类
// CppClass4Demo3Time 类中的成员函数可以访问 CppClass4Demo3Date 的 private 和 protected 属性和函数
string CppClass4Demo3Time::ShowTime(CppClass4Demo3Date &d)
{
d._year = ;
return int2string(d._year) + "-" + int2string(d._month) + "-" + int2string(d._day) + " " + int2string(_hour) + ":" + int2string(_minute) + ":" + int2string(_second);
} // 友元类的演示
void cppclass4_demo3()
{
// 注:
// 1、如果 B 是 A 的友元类,那么 A 不一定是 B 的友元类
// 2、如果 B 是 A 的友元类,C 是 B 的友元类,那么 C 不一定是 A 的友元类 CppClass4Demo3Time t(, , );
CppClass4Demo3Date d(, , );
string result = t.ShowTime(d); // 2016-4-3 8:30:15
}
OK
[源码下载]
不可或缺 Windows Native (20) - C++: 友元函数, 友元类的更多相关文章
- 不可或缺 Windows Native (23) - C++: 虚函数
[源码下载] 不可或缺 Windows Native (23) - C++: 虚函数 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 虚函数 示例1.基类CppHuman ...
- 不可或缺 Windows Native (6) - C 语言: 函数
[源码下载] 不可或缺 Windows Native (6) - C 语言: 函数 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 函数 示例cFunction.h # ...
- 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)
[源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...
- 不可或缺 Windows Native (22) - C++: 多重继承, 虚基类
[源码下载] 不可或缺 Windows Native (22) - C++: 多重继承, 虚基类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 多重继承 虚基类 示例1 ...
- 不可或缺 Windows Native 系列文章索引
[源码下载] 不可或缺 Windows Native 系列文章索引 作者:webabcd 1.不可或缺 Windows Native (1) - C 语言: hello c 介绍不可或缺 Window ...
- 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板
[源码下载] 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板 作者:webabcd 介绍不可或缺 Windows Native 之 C++ ...
- 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换
[源码下载] 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自 ...
- 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native
[源码下载] 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native 作者:web ...
- 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板
[源码下载] 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板 作者:webabcd 介绍不可或缺 Window ...
随机推荐
- python Web开发框架-Django (1)
以前用web.py(另外一款轻量级web开发框架)做一个监控管理平台,没有做特别的记录就不好拾起来.最近做一个日志聚合系统,使用的是django,这次就记下来,方便查询. Django是一个高效的we ...
- Android 数据传递(一) Activity之间的数据传递
bundle Google Bundle类说明 Bundle类是一个key-value对.Activity之间的数据通信可以通过bundle类来实现数据的存储.即将数据放入bundle里面,将Bund ...
- vb6里面dim和set的区别
dim是作用于变量 声明变量并分配存储空间 set作用于对象 将对象引用赋给变量或属性 例子: dim A as collection set A=new collection 等效于 di ...
- Android后台保活实践总结:即时通讯应用无法根治的“顽疾”
前言 Android进程和Service的保活,是困扰Android开发人员的一大顽疾.因涉及到省电和内存管理策略,各厂商基于自家的理解,在自已ROOM发布于都对标准Android发行版作为或多或少的 ...
- HTTP协议从入门到大牛,初识HTTP协议(学习笔记)
HTTP数据传输协议 当访问一个网页时,浏览器会向服务器发起一条HTTP请求,接着服务器会去寻找相应的资源,如果请求成功,就会把这个对象,对象类型,对象长度以及其他的信息放在HTTP响应中,发送给客户 ...
- Command /usr/bin/codesign failed with exit code 1
刚刚碰到相同的问题,自己解决了,很简单,profile冲突,(自己遇到的现象是之前的profile关联的certificate过期,然后重新生成 了certificate和更新了profile.但是是 ...
- android rectF
new Rect(left , top, right , bottom) 这个构造方法需要四个参数这四个参数 指明了什么位置 ?我们就来解释怎么画 这个 矩形 这四个 参数 分别代表的意思是:left ...
- Cocos2d-x 3.2 学习笔记(六)Layer
Layer 游戏中的背景容器,Layer类是Node类的一个子类,它实现了触屏事件代理(TouchEventsDelegate)协议. LayerColor是Layer的一个子类,它实现了RGBAPr ...
- 拓扑排序(一)之 C语言详解
本章介绍图的拓扑排序.和以往一样,本文会先对拓扑排序的理论知识进行介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现. 目录 1. 拓扑排序介绍 2. 拓扑排序的算法图解 3. 拓扑 ...
- 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...