[源码下载] 不可或缺 Windows Native (20) - C++: 友元函数, 友元类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 友元函数 友元类 示例演示友元函数, 友元类CppClass4.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppClass4 { public: string Demo(); }; } CppClas…
[源码下载] 不可或缺 Windows Native (23) - C++: 虚函数 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 虚函数 示例1.基类CppHuman.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppHuman { protected: string Name; public: // 我是虚函数 virtual string…
[源码下载] 不可或缺 Windows Native (6) - C 语言: 函数 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 函数 示例cFunction.h #ifndef _MYHEAD_FUNCTION_ #define _MYHEAD_FUNCTION_ #ifdef __cplusplus extern "C" #endif // 函数声明 // 像这种在 .h 中声明的函数,如果想被外部文件调用的话,则外部文件不用再声明即可调用 cha…
[源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员) 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 继承 组合 派生类的构造函数和析构函数 基类与派生类的转换 子对象的实例化 基类成员的隐藏(派生类成员覆盖基类成员) 示例1.基类CppAnimal.h #pragma once #include <string>…
[源码下载] 不可或缺 Windows Native (22) - C++: 多重继承, 虚基类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 多重继承 虚基类 示例1.基类 1CppBase1.h #pragma once #include <string> #include "CppBaseVirtual.h" using namespace std; namespace NativeDll { // virtual 代表 CppBase…
[源码下载] 不可或缺 Windows Native 系列文章索引 作者:webabcd 1.不可或缺 Windows Native (1) - C 语言: hello c 介绍不可或缺 Windows Native 之 C 语言 在 Windows Store Apps 中调用 C/C++ hello c 2.不可或缺 Windows Native (2) - C 语言: 常量,变量,基本数据类型 介绍不可或缺 Windows Native 之 C 语言 常量 变量 基本数据类型 3.不可或缺…
[源码下载] 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 函数重载 缺省参数 内联函数 函数模板 示例1.函数重载, 缺省参数CppFunction1.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppFunction1 { publ…
[源码下载] 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自定义类型转换 示例CppOperator.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppOperator { public: string Demo(); }; } CppO…
[源码下载] 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native 作者:webabcd 介绍不可或缺 Windows Native 之 C++ windows app native android app native ios app native 示例一.演示 windows app native 开发1.native 层CppCx.h #pragma once #incl…
[源码下载] 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 对象的动态创建和释放 对象的赋值和复制 静态属性和静态函数 类模板 示例1.CppEmployee 类CppEmployee.h #pragma once #include <string> using namespace std; namespace NativeD…
[源码下载] 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象,  const 指针和指向 const 对象的指针, const 对象的引用 作者:webabcd 介绍不可或缺 Windows Native 之 C++ this 指针 对象数组 对象和指针 const 对象 const 指针和指向 const 对象的指针 const 对象的引用 示例1.CppEmployee 类CppEmployee.h #pragma…
[源码下载] 不可或缺 Windows Native (17) - C++: 类与对象 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 类与对象 示例1.类的设计CppEmployee.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppEmployee { int Number; // 默认为 private private: // 以下都是 p…
[源码下载] 不可或缺 Windows Native (15) - C++: 命名空间 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 命名空间 示例CppNamespace.h #pragma once #include <string> using namespace std; // 定义一个命名空间,并在其中定义一个类以及声明一个函数 namespace NativeDll { class CppNamespace { public: string Demo…
[源码下载] 不可或缺 Windows Native (14) - C++: 文件 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 文件 示例CppIO2.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppIO2 { public: string Demo(string rootPath); }; } CppIO2.cpp /* * 文件 */…
[源码下载] 不可或缺 Windows Native (13) - C++: 标准输入, 标准输出, 字符串内存流 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 标准输入 标准输出 字符串内存流 示例1.标准输入, 标准输出CppIO1.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppIO1 { public: string Demo();…
[源码下载] 不可或缺 Windows Native (12) - C++: 引用类型 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 引用类型 示例CppReference.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppReference { public: string Demo(); }; } CppReference.cpp /* *…
[源码下载] 不可或缺 Windows Native (11) - C++: hello c++, C++ 与 C语言的区别小介 作者:webabcd 介绍不可或缺 Windows Native 之 C++ hello c++ C++ 与 C语言的区别小介 示例1.hello c++CppHello.h // 保证文件只被编译一次(即使被多次引用,也只被编译一次) /* * #ifndef 的方式依赖于宏名字不能冲突 * #pragma once 保证同一个文件不会被多次编译,这里的“同一个文件…
[源码下载] 不可或缺 Windows Native (1) - C 语言: hello c 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 在 Windows Store Apps 中调用 C/C++ hello c 示例1.演示如何在 Windows Store Apps(C#) 中调用 C/C++,需要新建 Windows Runtime Component(C++) 项目NativeDll/Simple.h /* * .h 头文件 */ // 保证文件只被…
[源码下载] 不可或缺 Windows Native (3) - C 语言: 运算符,表达式,条件语句,循环语句,转向语句,空语句等 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 运算符 表达式 条件语句 循环语句 转向语句 空语句 示例1.运算符cOperator.h #ifndef _MYHEAD_OPERATOR_ #define _MYHEAD_OPERATOR_ #ifdef __cplusplus extern "C" #endif cha…
[源码下载] 不可或缺 Windows Native (4) - C 语言: 预处理命令,输入,输出 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 预处理命令 输入 输出 示例1.预处理命令cPreprocessor.h #ifndef _MYHEAD_PREPROCESSOR_ #define _MYHEAD_PREPROCESSOR_ #ifdef __cplusplus extern "C" #endif char *demo_cPreproce…
[源码下载] 不可或缺 Windows Native (7) - C 语言: 指针 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 指针 示例cPointer.h #ifndef _MYHEAD_POINTER_ #define _MYHEAD_POINTER_ #ifdef __cplusplus extern "C" #endif char *demo_cPointer(); #endif cPointer.c /* * 指针 */ #include…
[源码下载] 不可或缺 Windows Native (8) - C 语言: 结构体,共用体,枚举,类型定义符 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 结构体 共用体 枚举 类型定义符 示例cStruct.h #ifndef _MYHEAD_STRUCT_ #define _MYHEAD_STRUCT_ #ifdef __cplusplus extern "C" #endif char *demo_cStruct(); #endif cStruc…
[源码下载] 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 动态分配内存 链表 位域 示例cMemory.h #ifndef _MYHEAD_MEMORY_ #define _MYHEAD_MEMORY_ #ifdef __cplusplus extern "C" #endif char *demo_cMemory(); #endif cMemory.c /* *…
[源码下载] 不可或缺 Windows Native (10) - C 语言: 文件 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 文件 示例cFile.h #ifndef _MYHEAD_FILE_ #define _MYHEAD_FILE_ #ifdef __cplusplus extern "C" #endif char *demo_cFile(char *rootPath); #endif cFile.c /* * 文件 * * 从用户角度讲,…
[源码下载] 不可或缺 Windows Native (2) - C 语言: 常量,变量,基本数据类型 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 常量 变量 基本数据类型 示例cDataType.h #ifndef _MYHEAD_DATATYPE_ #define _MYHEAD_DATATYPE_ #ifdef __cplusplus extern "C" #endif char *demo_cDataType(); char *demo_co…
[源码下载] 不可或缺 Windows Native (5) - C 语言: 数组 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 数组 示例cArray.h #ifndef _MYHEAD_ARRAY_ #define _MYHEAD_ARRAY_ #ifdef __cplusplus extern "C" #endif char *demo_cArray(); #endif cArray.c /* * 数组 * * * 定义数组,int ary[10…
根据<C++ Primer>第三版16.4节的叙述,C++类模板友元分为以下几种情况 1.非模板友元类或友元函数. 书上给了一个例子: class Foo{     void bar(); }; template <class T> class QueueItem{     friend class foobar;     friend void foo();     friend void Foo::bar();     //.... }; 很简单,跟非模板类没什么区别,有一点需…
友元函数和友元类的需要: 类具有封装和信息隐藏的特性.只有类的成员函数才能访问类的私有成员,程序中的其他函数是无法访问私有成员的.非成员函数可以访问类中的公有成员,但是如果将数据成员都定义为公有的,这又破坏了隐藏的特性.另外,应该看到在某些情况下,特别是在对某些成员函数多次调用时,由于参数传递,类型检查和安全性检查等都需要时间开销,而影响程序的运行效率. 为了解决上述问题,提出一种使用友元的方案.友元是一种定义在类外部的普通函数,但它需要在类体内进行说明,为了与该类的成员函数加以区别,在说明时前…
友元函数 默认一个类的私有属性只能在该类的内部可以直接访问.友元函数申明在内的内部,实现在类的外部可以直接访问类的私有属性. class A1 { public: A1() { a1 = 100; a2 = 200; } int getA1() { return this->a1; } //声明一个友元函数 friend void setA1(A1 *p, int a1); //这个函数是这个类的好朋友 protected: private: int a1; int a2; }; void se…
1.友元函数的简单介绍 1.1为什么要使用友元函数 在实现类之间数据共享时,减少系统开销,提高效率.如果类A中的函数要访问类B中的成员(例如:智能指针类的实现),那么类A中该函数要是类B的友元函数. 具体来说:为了使其他类的成员函数直接访问该类的私有变量.即:允许外面的类或函数去访问类的私有变量和保护变量,从而使两个类共享同一函数. 实际上具体大概有下面两种情况需要使用友元函数:(1)运算符重载的某些场合需要使用友元.(2)两个类要共享数据的时候. 1.2使用友元函数的优缺点 优点:能够提高效率…