不可或缺 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 ...
随机推荐
- 在设置代理的环境下使用SharePoint CSOM
SharePoint 的CSOM都是通过HttpRequest来实现和SharePoint服务器的交互的,那么我们如何设置HttpWebRequest的一些特性呢,如Cookie,WebProxy? ...
- html5 postMessage解决跨域、跨窗口消息传递
一些麻烦事儿 平时做web开发的时候关于消息传递,除了客户端与服务器传值还有几个经常会遇到的问题 1.页面和其打开的新窗口的数据传递 2.多窗口之间消息传递 3.页面与嵌套的iframe消息传递 4. ...
- CGLib与JDKProxy的区别
Spring AOP 的实现主要有两种:CGLib与JDK自带的Proxy. 他们主要的区别是,需要JDKProxy修改的类必须实现接口(因此也只能代理public方法),在创建Proxy时可以使用c ...
- cxf restful
Restful 服务端 1 创建好pojo.dao.service, dao进行数据库操作,service提供服务 @Path("/roomservice") @Produces( ...
- Atitit 混合叠加俩张图片的处理 图像处理解决方案 javafx blend
Atitit 混合叠加俩张图片的处理 图像处理解决方案 javafx blend 1.1. Jhlabs 好像有了可以叠加算法,但是没有找到doc1 1.2. 自己算法叠加1 1.3. 使用javaf ...
- vue.js学习之入门实例
之前一直看过vue.js官网api,但是很少实践,这里抽出时间谢了个入门级的demo,记录下一些知识点,防止后续踩坑,牵扯到的的知识点:vue.vue-cli.vue-router.webpack等. ...
- 高性能Cordova App开发学习笔记
高性能Cordova App开发学习笔记 文件结构 添加插件 构建准备 各个www的作用,prepare命令会将hello\www的内容会拷贝到platform下的wwww目录,知道该改哪里了吧?如果 ...
- Python中的字符串与字符编码
本节内容: 前言 相关概念 Python中的默认编码 Python2与Python3中对字符串的支持 字符编码转换 一.前言 Python中的字符编码是个老生常谈的话题,同行们都写过很多这方面的文章. ...
- 初次使用IDEA的相关技巧
前言:由于初次使用IDEA,所以很多配置都不是非常熟悉,经过一下午慢慢熟悉和同事的帮助,终于有所斩获,现在我把这个总结写出来,希望能够帮助初次使用的java工程师. 1:下载和安装 下载地址:http ...
- CSS隐藏元素的N种实现方式。
1. width:0; 光有高度是不行的,还得有宽度.缺点文字隐藏不了,可以加个color:#fff和背景颜色一样就ok了,障眼法,迷惑人的,其实内容还在,如果有文字的话,还是可以触发点击事件的,这种 ...