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

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. Android Drawable资源

    Android实现应用d动画效果:比如App第一次打开的开始动画等 有两种:GIF动画和代码实现. 第一种:借助于Gif制作工具软件实现.一般是和第三方开源的GifView(https://githu ...

  2. React添加事件

    定义个组件 组件首字母大写,调用: ReactDOM.render(<Hello></Hello>,document.getElementById('box'));

  3. jmeter笔记8

     JMETER接口性能测试方案 JMETER简介          JMeter可以用于测试静态或者动态资源的性能(文件.Servlets.Perl脚本.java对象.数据库和查询.ftp服务器或者其 ...

  4. nodeschool.io 6

    ~~ MAKE IT MODULAR ~~ This problem is the same as the previous but introduces the concept ofmodules. ...

  5. (13)odoo翻译

    -------------------更新时间:15:52 2016-09-28 星期三 增加模型名翻译17:26 2016-05-20 星期五17:58 2016-05-17 星期二12:14 20 ...

  6. Feistel密码结构

    分组密码:是一种加解密方案,将输入的明文分组当作一个整体出来,输出一个等长的密文分组. 典型的分组大小为64位和128位.密钥长度一般为128位.迭代轮数典型值为16轮. Feistel 密码结构是用 ...

  7. js-分享107个js中的非常实用的小技巧(借鉴保存)

    转载原文:http://***/Show.aspx?id=285 1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:doc ...

  8. php中PCRE正则表达式分隔符的使用

    转自:http://www.baiwar.com/post/the-use-of-php-pcre-regex-delimiter.html 在php5.3.0以前,PHP可使用两套正则表达式规则,一 ...

  9. 隐藏chrome空白标签栏的最近访问

    chrome版本: 29.0.1547.76 m 找到安装路径下Custom.css文件,添加.most-visited{display:none !important}来修改样式. 我的路径为:C: ...

  10. UESTC 2016 Summer Training #1 Div.2

    最近意志力好飘摇..不知道坚不坚持得下去.. 这么弱还瞎纠结...可以滚了.. 水题都不会做.. LCS (A) 水 LCS (B) 没有看题 Gym 100989C 水 1D Cafeteria ( ...