小编Ste*_* B.的帖子

C++ 通过引用返回和通过常量引用返回值被复制

我有一些关于重新获取类成员变量的引用的问题。

我有以下代码:

#include <stdint.h>
#include <string>
#include <iostream>
#include <set>

void
PrintSet (const std::string & str, const std::set<uint32_t> & to_print)
{
  std::cout << str << " (" << to_print.size () << "): ";

  for (std::set<uint32_t>::const_iterator it = to_print.begin ();
          it != to_print.end (); ++it)
    {
      std::cout << *it << " ";
    }

  std::cout << "\n";
}

class Test
{
private:
  std::set<uint32_t> m_values;

public:

  Test () : m_values () { }

  void
  SetValues (const std::set<uint32_t> & values)
  {
    m_values = values; …
Run Code Online (Sandbox Code Playgroud)

c++ return reference const-reference return-by-reference

2
推荐指数
2
解决办法
7622
查看次数