首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > More Effective C++ -- Tip 25
【标  题】:More Effective C++ -- Tip 25
【关键字】:More,Effective,C++,--,Tip
【来  源】:http://www.cublog.cn/u/23794/showart.php?id=169604

More Effective C++ -- Tip 25

第一篇blog就写写读书笔记吧。
 
constructor的虚化:
所谓virtual constructor,就是根据其输入,可以产生不同类型的对象。这个有点类似于工厂模式。 其本质上利用了虚函数返回类型的宽松点,即虚函数的返回类型可以是指向base class的指针或者引用。
 
Example:
class NLComponent
{
public:
    virtual NLComponent *clone() const = 0;
    ...
};
 
class TextBlock : public NLComponent
{
public:
    virtual TextBlock *clone() const
    {  return new TextBlock(*this); }
};
 
class Graphic : public NLComponent
{
public:
    virtual Graphic *clone() const
    {  return new Graphic(*this); }
};
 
 
Non-member function的虚化:
原理很简单,但是未必人人都能想到并用好: 写一个虚函数做实际工作,在写一个仅调用这个虚函数的Non-member function. 可以利用inline来提高效率。
 
Example:
class NLComponent
{
public:
    virtual ostream & print(ostream & s) const = 0;
};
 
class TextBlock : public NLComponent
{
public:
    virtual ostream & print(ostream & s) const;
};
 
class Graphic : public NLComponent
{
public:
    virtual ostream & print(ostream & s) const;
};
 
inline ostream & operator<<(ostream & s, const NLComponent & c)
{
    return c.print(s);
}
 
 
That's all.
 
有空再写。
RAC中crs的svrctl 的使用:【上一篇】
[点评][东南大学C++]:【下一篇】
【相关文章】
  • 再谈DBA---到底是鸡生蛋还是蛋生鸡
  • 关于子网掩码计算 ---ip地址计算----ip地址分配
  • netcat---windows版的详细说明
  • 推荐:NC的反弹shell---动画
  • 如何记录键盘操作--ZT
  • 还是那样的日子---在学校里
  • 在编程中常用的工具--今天学习的重点
  • 学习LINUX之前--操作系统
  • 内核_.config 内核配置[ZT]--make menuconfig
  • Solaris2.4 多线程编程指南1--线程基础
  • 【随机文章】
  • C++ Builder 编写动作组件
  • ceiling
  • 如何编写 linux 设备驱动程序
  • 商业flash设计经验谈(一)
  • [转]AWK:Linux管理员的智能工具包
  • tomcat性能分析与优化
  • 如何删除GRUB启动管理器
  • VoIP + WLAN 一种理想的新组合
  • Photoshop美食系列之瑞士乳酪
  • 对错误的态度
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.