当前位置: 首页 > news >正文

【C++ techniques】虚化构造函数、虚化非成员函数

constructor的虚化

  • virtual function:完成“因类型而异”的行为;
  • constructor:明确类型时构造函数;
  • virtual constructor:视其获得的输入,可产生不同的类型对象。
//假如写一个软件,用来处理时事新闻,其内容由文字和图形构成class NTComponent{   //抽象基类,用于时事消息的组件
public:				 //其中至少含有一个纯虚函数...
};class TextBlock:public NTComponent{
public:...				//没有内含有任何纯虚函数
};	class Graphic : public NTComponent{
public:...				//没有内含有任何纯虚函数
};		class NewsLetter{	//一份时事通讯是有一系列的NTComponent对象构成
public:...
private:list<NTComponent*> components;
};	
//NewsLetter对象尚未开始运作的时候,可能存储于磁盘中
//让NewsLetter拥有一个constructor并用istream作为自变量
//这个constructor将从stream读取数据以便产生必要的核心数据结构:class NewsLetter{
public:NewsLetter(istream& str);...
};NewsLetter::NewsLetter(istream& str)
{while(str){read the next component object;add the object to the list of this newsletter's components;}
}
//如果将棘手的东西搬移到另一个名为readComponent的函数:class NewsLetter{
public:...
private:static NTComponent* readComponent(istream& str);...                                             
};NewsLetter::NewsLetter(istream& str)
{while(str){//将readComponent返回的指针加到component list尾端,//“push_back”是一个list member function,用来将对象安插//到list尾端components.push_back(readComponent(str));}
}

此时readComponent产生新对象,所以行为仿若constructor,但是它能够产生不同类型的对象,所以我们称之它为一个virtual constructor

virtual constructor:

  • 某种函数,视其获得的输入,可产生不同的类型对象;
  • 在许多情况下有用,比如从磁盘(或网盘或磁带等)读取对象信息。

virtual copy constructor:

  • 返回一个指针,指向其调用者(某对象)的一个新副本;
  • 基于这种行为,virtual copy constructors通常以copySelf或cloneSelf命名,或者像下面一样命令为clone。
class NLComponent{
public://声明 virtual copy constructorvirtual NLComponent* clone() const = 0;...
};class TextBlock:public NLComponent{
public:virtual TextBlock* clone() const //virtual copy constructor{return new TextBlock(*this);}...
};class Graphic:public NLComponent{
public:virtual Graphic* clone() const //virtual copy constructor{return new Graphic(*this);}...
};
class NewsLetter{
public:NewsLetter(const NewsLetter& rhs);...
private:list<NLComponent*> components;
};NewsLetter::NewsLetter(const NewsLetter& rhs)
{//迭代遍历rhs list,运用每个元素的virtual copy constructor,//将元素复制到对象的component list中。for(list<NLComponent*>::const_iterator it = rhs.components.begin();it != rhs.components.end(),++it){components.push_back((*it)->clone());}
}

non-member functions的虚化

写一个虚函数做实际工作,再写一个什么都不做的非虚函数,只负责调用虚函数;为了避免此巧妙安排蒙受函数调用所带来的成本,也可以将非虚函数inline化。

//为TextBlock和Graphic实现出output操作符class NLComponent{
public:virtual ostream& print(ostream& str) const = 0;
};class TextBlock:public NLComponent{
public:virtual ostream& print(ostream& str);
};class Graphic:public NLComponent{
public:virtual ostream& print(ostream& str);
};inline ostream& operator<<(ostream& s,const NLComponent& c)
{return c.print(s);
}
http://www.lryc.cn/news/182289.html

相关文章:

  • 蓝牙核心规范(V5.4)11.6-LE Audio 笔记之初识音频位置和通道分配
  • mysql双主+双从集群连接模式
  • 嵌入式中如何用C语言操作sqlite3(07)
  • RandomForestClassifier 与 GradientBoostingClassifier 的区别
  • 计组——I/O方式
  • jsbridge实战2:Swift和h5的jsbridge通信
  • 集合原理简记
  • 机器学习的超参数 、训练集、归纳偏好
  • Leetcode1071. 字符串的最大公因子(三种方法,带详细解析)
  • 如何像人类一样写HTML之图像标签,超链接标签与多媒体标签
  • 1300*C. Rumor(并查集贪心)
  • python实用小代码(数据分析向)
  • 【oncmdmsg 鼠标】2023/8/19 上午9:50:14
  • 插入排序:简单而有效的排序方法
  • OpenGL之光照贴图
  • 隐私交易成新刚需,Unijoin 凭什么优势杀出重围?
  • 小谈设计模式(12)—迪米特法则
  • Foxit PDF
  • 《Python趣味工具》——ppt的操作(刷题版)
  • 实战型开发--3/3,clean code
  • 家用无线路由器如何用网线桥接解决有些房间无线信号覆盖不好的问题(低成本)
  • 【Golang】网络编程
  • 使用策略模式优化多重if/else
  • 逆强化学习
  • postgresql新特性之Merge
  • 【注解】注解解析与应用场景
  • mysql面试题14:讲一讲MySQL中什么是全同步复制?底层实现?
  • Linux驱动设备号分配与自动创建设备节点
  • 基于MFC和OpenCV实现人脸识别
  • 力扣 -- 377. 组合总和 Ⅳ