一般而言,友元作为一种紧耦合的设计,被视作类的一部分。一个经典的应用就是关于类的序列化和反序列化。

class A
{
friend ostream& operator<<(ostream& os, const A& a);
private:
int i;
} ostream& operator<<(ostream& os, const A& a)
{
return os << a.i;
}

问题是当在一个继承结构里面,每个类都需要这么一个序列化(反序列化)过程的时候,每次重新写一个友元函数实在是繁琐。

但是友元函数又不是类的成员函数,不能定义成虚函数。于是在C++提供了这样一种语法支持:在友元函数里面调用虚函数,使得

它能够呈现出多态特性:

    class Base {
public:
friend std::ostream& operator<< (std::ostream& o, const Base& b);
// ...
protected:
virtual void printOn(std::ostream& o) const = ; // Or plain virtual; see below
};
inline std::ostream& operator<< (std::ostream& o, const Base& b)
{
b.printOn(o);
return o;
}
class Derived : public Base {
public:
// ...
protected:
virtual void printOn(std::ostream& o) const;
};
void Derived::printOn(std::ostream& o) const
{
// ...
}

这样,我们只要重载成员函数printOn,就能直接使得我们定义的派生类具备了序列化(反序列化)的能力。

问题是这里为什么不直接将printOn定义成public的,然后直接用它呢?

我能理解出的一个优点就是统一性,所有类都去定义这样的序列化操作符,比起各自设计不同的成员函数要强。

Virtual Friend Function的更多相关文章

  1. Standard C++ Programming: Virtual Functions and Inlining

    原文链接:http://www.drdobbs.com/cpp/standard-c-programming-virtual-functions/184403747 By Josée Lajoie a ...

  2. (C/C++ )Interview in English - Virtual

    Q: What is virtual function?A: A virtual function or virtual method is a function or method whose be ...

  3. c++virtual inline 是否冲突

    关于inline关键字:effective c++ item33:明智运用inlining.说到:inline指令就像register指令一样,只是对编译器的一种提示,而不是一个强制命令,意思是编译器 ...

  4. Function语义学之member function

    之前我们讲过编译器会对 nonmember functions 进行怎样的扩充和该写,今天我们来讲一下 member functions 函数调用方式 一.Nonstatic Member Funct ...

  5. Function 语意学

    C++支持三种类型的member functions: static.nonstatic和virtual,每一种类型调用方式都不相同. 一 nostatic members functions 1 调 ...

  6. c++ virturn function -- 虚函数

    c++ virturn function -- 虚函数 pure irtual function  -- 纯虚函数   先看例子 #include <iostream> using nam ...

  7. 《深度探索C++对象模型》笔记——Function语意学

    member的各种调用方式 C++支持三种类型的member functions:static.nonstatic和virtual. nonstatic member functions会被编译器转换 ...

  8. Using C++11 function & bind

    The example of callback in C++11 is shown below. #include <functional> void MyFunc1(int val1, ...

  9. [翻译] Virtual method interception 虚方法拦截

    原文地址:http://blog.barrkel.com/2010/09/virtual-method-interception.html 注:基于本人英文水平,以下翻译只是我自己的理解,如对读者造成 ...

随机推荐

  1. 【poj2728】Desert King

    [poj2728]Desert King 题意 最优比率生成树. http://blog.csdn.net/ophunter_lcm/article/details/10113817 分析 Dinke ...

  2. js解码

    如代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  3. echarts折线图--数据交互

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. each用法

    1.数组用法 <script> var s=["s","i","l","e","n",& ...

  5. 什么是 Unix 以及它为什么这么重要?

    大多数操作系统可以被划分到两个不同的家族.除了微软的基于Windows NT的操作系统外,几乎所有其他的都可以追溯到Unix. Linux,Mac OS X,Android,iOS,Chrome OS ...

  6. qml 一些知识点

    1.pagestack进行页面调整的时候,需要对页面状态做一些跟踪: Stack.onStatusChanged: { if (Stack.status == Stack.Active) { //可以 ...

  7. 监听TelephonyManager的通话状态来监听手机的所有的来电

    import java.io.FileNotFoundException;import java.io.OutputStream;import java.io.PrintStream;import j ...

  8. redis sort

    redis sort命令用法 1.命令描述返回或保存给定列表.集合.有序集合key中经过排序的元素.排序默认以数字作为对象,值被解释为双精度浮点数,然后进行比较. 2.一般sort用法最简单的sort ...

  9. P121 6.7 第一题和第二题

    package nothh; import java.util.Arrays; public class shuzu6_7 { public static void main(String[] arg ...

  10. myeclipse9.x,10.x 安装时出现pulse-one-64,failed to load the JNI shared library