代码之家  ›  专栏  ›  技术社区  ›  nacho4d

是否可以在不进行子类化的情况下向类中添加一些功能?

c++
  •  2
  • nacho4d  · 技术社区  · 15 年前

    在ojective-c中,有一个叫做categories的东西,它允许用户从原始.h或.m文件(objective-c的.cpp版本)之外添加方法。 我想知道C++中是否存在这样的功能。

    我特别想使用implement<<operator进行调试,或者我经常使用的库中的其他类。(而且不想使用宏,因为它看起来很难看;)

    谢谢。

    3 回复  |  直到 15 年前
        1
  •  5
  •   kennytm    15 年前

    std::ostream& operator<< (std::ostream& f, const YourClass& cls) {
      ...
    }
    

    friend

        2
  •  4
  •   reko_t    15 年前

    ostream& operator <<(ostream& lhs, const SomeClass& rhs) {
        /* Output something to lhs using rhs object */
        return lhs;
    }
    

    friend