这是否是实现<

问题描述 投票:1回答:2

假设您具有以下代码:

#include <iostream>

template <typename T>
class Example
{
    public:
        Example() { };
        Example(const T &_first_ele, const T &_second_ele) : first_(_first_ele), 
        second_(_second_ele) { }

        friend std::ostream &operator<<(std::ostream &os, const Example &a)
        {
            return (os << a.first_ << " " << a.second_);
        }

    private:
        T first_;
        T second_;
};

int main()
{
    Example<double> example_(3.45, 24.6);
    std::cout << example_ << "\n";
    return 0;
}

这是过载operator<<的唯一方法吗?

    friend std::ostream &operator<<(std::ostream &os, const Example &a)
    {
        return (os << a.first_ << " " << a.second_);
    }

就性能而言,这是重载它的最佳方法,还是有更好的选择来实现此实现?

c++ operator-overloading c++14 c++17 ostream
2个回答
0
投票

这是实现它的明显方法。它可能也是最有效的。使用它。


0
投票

您在问题中演示的方式是最初级的方式,可以在各种C ++书籍中找到。但是,在生产代码中,这当然不是最好的方法。

  • 主要是因为您必须为每个类编写friend operator<<的样板代码。
  • 添加新的类成员时,您可能还必须分别更新方法。

自C ++ 11起,我将推荐以下方式:

图书馆

template<typename Derived> // Util_ostream.hpp
struct ostream
{
  std::function<std::ostream&(std::ostream&)> m_fOstream;

  template<typename T, typename... Args>
  void Output (const T& separator, const Args&... args)
  {
    m_fOstream = [separator, args...] (std::ostream& os) -> std::ostream&
                 {
                   os << "(" << separator;
                   int unused[] = { (os << args << separator, 0) ... }; (void) unused;
                   return os << ")";
                 };
  }

  friend std::ostream& operator<< (std::ostream& os, const Derived& derived)
  {
    return derived.m_fOstream(os);
  }
};

用法

继承您要为其operator<<便利的类的上述类。自动friend将进入这些类的定义。因此,每次都无需额外的工作。例如

class MyClass : public ostream<MyClass> {...};

在构造函数中的某处,您可以Attach()要打印的那些变量。例如

MyClass () { this->Attach("\n----\n", this->x, this->y); }

示例

根据您分享的内容,>

#include"Util_ostream.hpp"

template<typename T>
class Example : public ostream<Example<T>> // change 1
{
public:
  Example(const T &_first_ele, const T &_second_ele) : first_(_first_ele), second_(_second_ele) 
  { this->Attach(" ", first_, second_); }  // change 2

private:
  T first_;
  T second_;
};

Demo


在性能上,上面的代码应该与您所显示的代码几乎相同,除了调用std::function的指令可以忽略不计。另一方面,它提供了避免编写过多代码的巨大好处。

© www.soinside.com 2019 - 2024. All rights reserved.