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

178 阅读1分钟
  • 所谓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();
}