Virtual Friend Function
一般而言,友元作为一种紧耦合的设计,被视作类的一部分。一个经典的应用就是关于类的序列化和反序列化。
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的更多相关文章
- Standard C++ Programming: Virtual Functions and Inlining
原文链接:http://www.drdobbs.com/cpp/standard-c-programming-virtual-functions/184403747 By Josée Lajoie a ...
- (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 ...
- c++virtual inline 是否冲突
关于inline关键字:effective c++ item33:明智运用inlining.说到:inline指令就像register指令一样,只是对编译器的一种提示,而不是一个强制命令,意思是编译器 ...
- Function语义学之member function
之前我们讲过编译器会对 nonmember functions 进行怎样的扩充和该写,今天我们来讲一下 member functions 函数调用方式 一.Nonstatic Member Funct ...
- Function 语意学
C++支持三种类型的member functions: static.nonstatic和virtual,每一种类型调用方式都不相同. 一 nostatic members functions 1 调 ...
- c++ virturn function -- 虚函数
c++ virturn function -- 虚函数 pure irtual function -- 纯虚函数 先看例子 #include <iostream> using nam ...
- 《深度探索C++对象模型》笔记——Function语意学
member的各种调用方式 C++支持三种类型的member functions:static.nonstatic和virtual. nonstatic member functions会被编译器转换 ...
- Using C++11 function & bind
The example of callback in C++11 is shown below. #include <functional> void MyFunc1(int val1, ...
- [翻译] Virtual method interception 虚方法拦截
原文地址:http://blog.barrkel.com/2010/09/virtual-method-interception.html 注:基于本人英文水平,以下翻译只是我自己的理解,如对读者造成 ...
随机推荐
- bzoj题解汇总(1021~1031)
bzoj1021:普通dp bzoj1022:裸的Anti-Nim 必胜:①sg=0且所有不超过1 ②sg>1且存在至少一个超过1 bzoj1023:http://www.cnblogs.com ...
- DispatcherServlet
<servlet> <servlet-name>chapter2</servlet-name> <servlet-class>org.springfra ...
- css写宽为30%的正方形
如何用纯css写一宽为30%的正方形,用到了padding属性: 会不会恍然大悟呢? <!DOCTYPE html> <html lang="en"> &l ...
- 用excel处理重复数据
我们在处理数据时,重复数据常常会对分析造成很大麻烦,因此数据整理初期一个重要的工作是排重,excel2007以上版本中有一个删除重复项功常便捷,但是每次点来点去也很麻烦,下面我们用公式来对一些重复数据 ...
- Unique Binary Search Trees [LeetCode]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 11 自定制shell提示符
shell提示符 huiubantu@ubuntu:~$ shell提示符保存在PS1变量中 包括用户名,主机名,当前工作目录 可以通过echo命令查看PS1的内容 huiubantu@ubuntu ...
- PHP函数——parse_ini_file() 函数
资料网址:http://www.w3school.com.cn/php/func_filesystem_parse_ini_file.asp 1.parse_ini_file() 函数解析一个配置文件 ...
- Html5编辑工具
如果你是一名Web开发人员,当你需要开发一个独特的网站时,你就会知道文本编辑器的重要性.小编为大家整理了8款非常前沿的HTML5文本编辑器,简化开发流程,喜欢就转走吧! Mercury Editor ...
- Android 主页面顶部栏的通知Notification ,可以自定义通知消息栏的风格,并且点击通知栏进人本程序。
常用的程序通知,显示到主页面的顶部栏. package com.lixu.tongzhi; import android.app.Activity; import android.app.Notifi ...
- C# 如何用计时器Timer控件实现停留几秒再做切换窗体的操作
C# Timer用法及实例详解 关于C# Timer类 在C#里关于定时器类就有3个 C# Timer使用的方法1.定义在System.Windows.Forms里 C# Timer使用的方法2.定 ...