【More Effective C++】条款25:将构造函数和非成员函数虚拟化

构造函数虚化:

  • NewsLetter构造函数,能根据不同数据建立不同类型的对象,行为与构造函数相似;
class NLComponent {};
class TextBlock: public NLComponent {};
class Graphic: public NLComponent {};
class NewsLetter {
public:
    NewsLetter(std::istream& str) {
        while (str) {
            components.push_back(readNLComponent(str));
        }
    }
private:
    std::list<NLComponent*> components;
    static NLComponent* readNLComponent(std::istream& str);
};

拷贝构造函数虚化:

  • NewsLetter拷贝构造函数,能根据不同类型的对象,行为与拷贝构造函数相似;
  • 虚拟拷贝构造返回一个指针,只想调用调用该函数对象的新拷贝;
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);
    }
};
class NewsLetter {
public:
    NewsLetter(std::istream& str) {
        while (str) {
            components.push_back(readNLComponent(str));
        }
    }
    NewsLetter(const NewsLetter& rhs) {
        for (auto it = rhs.components.begin(); it != rhs.components.end(); ++it) {
            components.push_back((*it)->clone());
        }
    }
private:
    std::list<NLComponent*> components;
    static NLComponent* readNLComponent(std::istream& str);
};

非成员函数虚化:

  • 如果以成员函数的方式重载operator<<,得把cout放到<<符号的右边,与一般用法相反;

  • 另一种方法是声明虚拟函数和重载 operator<<非成员函数,该非成员函数只调用虚拟函数;

class NLComponent {
public:
    virtual std::ostream& operator<<(std::ostream& str) const = 0;
};
class TextBlock: public NLComponent {
public:
    virtual std::ostream& operator<<(std::ostream& str) const {
        str << "Output TextBlock information here!\n";
        return str;
    }
};
class Graphic: public NLComponent {
public:
    virtual std::ostream& operator<<(std::ostream& str) const {
        str << "Output Graphic information here!\n";
        return str;
    }
};

int main() {
    TextBlock t;
    t << std::cout;
    return 0;
}
class NLComponent {
public:
    virtual std::ostream& print(std::ostream& str) const = 0;
};
class TextBlock: public NLComponent {
public:
    virtual std::ostream& print(std::ostream& str) const {
        str << "Output TextBlock information here\n";
        return str;
    }
};
class Graphic: public NLComponent {
public:
    virtual std::ostream& print(std::ostream& str) const {
        str << "Output Graphic information here\n";
        return str;
    }
};
inline
std::ostream& operator<<(std::ostream& s, const NLComponent& c) {
    return c.print(s);
}
int main() {
    
    TextBlock t;
    std::cout << t;
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨主任o_o

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值