《More Effective C++》技术篇——将constructor和non-member function虚化

本文探讨了虚拟构造函数的概念,特别是虚拟拷贝构造函数,它能够根据输入创建不同类型的对象副本。通过示例展示了如何在派生类中重写基类的虚函数,允许返回指向基类或派生类的指针。此外,还介绍了如何将非成员函数的行为进行虚拟化,以提高效率。文章以`NewsLetter`类为例,展示了如何在类中添加和打印组件,以及如何通过克隆实现对象复制。
  • 所谓virtual constructor是某种函数,视其获得的输入,可产生不同类型的对象。
  • 有一种特别的virtual constructor——所谓virtual copy constructor——也被广泛地运用、virtual copy constructor会返回一个指针,指向其调用者(某对象)的一个新副本。基于这种行为,virtual copy constructor通常以copyself或cloneself命令,或者直接命名为clone。
  • 当derived class重新定义其base class的一个虚函数时,不再需要一定得声明与原本相同的返回类型。如果函数的返回类型是个指针或引用,指向一个base class,那么derived class的函数可以返回一个指向该base class的derived class的指针或引用。这样就可以声明出像virtual copy constructor这样的函数。
  • non-member function的虚化十分容易:写一个虚函数做实际工作,再写一个什么都不做的非虚函数,只负责调用虚函数。为了减少函数调用成本,可以将非虚函数inline化。
  • 下面就是virtual copy constructor和virtual non-member function的例子。
#include <iostream>
#include <list>

class NLComponent
{
public:
    // 声明 virtual copy constructor
    virtual NLComponent *clone() const = 0;

    virtual std::ostream& print(std::ostream& s) const = 0;

    NLComponent() {}
    virtual ~NLComponent() {}
};

class TextBlock : public NLComponent
{
public:
    // virtual copy constructor
    TextBlock *clone() const override {return new TextBlock(*this);}

    std::ostream& print(std::ostream& s) const override
    {
        s << "I'm TextBlock" << std::endl;
        return s;
    }
};

class Graphic : public NLComponent
{
public:
    // virtual copy constructor
    Graphic *clone() const override {return new Graphic(*this);}

    std::ostream& print(std::ostream& s) const override
    {
        s << "I'm Graphic" << std::endl;
        return s;
    }
};

// 将 Non-Member Function的行为虚化
inline std::ostream& operator<<(std::ostream& s, const NLComponent& c)
{
    return c.print(s);
}

class NewsLetter
{
public:
    NewsLetter() {}
    ~NewsLetter() {}

    // copy constructor
    NewsLetter(const NewsLetter& rhs);

    void AddComponet(NLComponent* c);
    void PrintComponents();

private:
    std::list<NLComponent*> components;
};

NewsLetter::NewsLetter(const NewsLetter& rhs)
{
    for (auto it = rhs.components.begin(); it != rhs.components.end(); it++)
    {
        components.push_back((*it)->clone());
    }
}

void NewsLetter::AddComponet(NLComponent* c)
{
    components.push_back(c);
}

void NewsLetter::PrintComponents()
{
    for (auto it = components.begin(); it != components.end(); it++)
    {
        std::cout << *(*it);
    }
}

int main()
{
    TextBlock tb;
    Graphic gp;
    NewsLetter nl1;
    nl1.AddComponet(&tb);
    nl1.AddComponet(&gp);
    NewsLetter nl2(nl1);
    nl2.PrintComponents();
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值