std :: vector push_back与std :: move仍然复制对象

时间:2018-03-15 16:13:11

标签: c++

我有一个名为Socket的基类,然后是一个名为RwSocket的子类,以及一个名为RoSocket的子类。

我将两个子类的实例push_back到std :: vector中,并看到调用的复制构造函数。但是,当我只推送其中一个时,则不调用任何复制构造函数,只移动构造函数。我从不希望调用复制构造函数。

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;


class SocketInterface
{
  public:
  SocketInterface(const std::string &name)
   : m_name(name)
  {
    cout << m_name << " ctor" << this << endl;
  }
  SocketInterface(const std::string &&name)
     : m_name(std::move(name))
  {
    cout << m_name << " ctor2" << this << endl;
  }
  virtual ~SocketInterface()
  {
    cout << m_name << " dtor" <<  this << endl;
  }
  SocketInterface(const SocketInterface&rhs)
     : m_name(rhs.m_name)
  {
     cout << m_name << " cptor&" <<  this << endl;
  }
  SocketInterface(const SocketInterface&&rhs)
     : m_name(std::move(rhs.m_name))
  {
     cout << m_name << " mvtor&&" <<  this << endl;
  }
  virtual void write(const std::string&) const = 0;
  virtual std::string read() const = 0;

  protected:
  std::string m_name;
};

class RwSocket : public SocketInterface
{
  public:
  RwSocket(const std::string &name)
  : SocketInterface(name) {}
  virtual ~RwSocket() {}
  RwSocket(const RwSocket&rhs)
     : SocketInterface(rhs)
  {}
  RwSocket(const RwSocket&&rhs)
     : SocketInterface(std::move(rhs))
  {}

  virtual void write(const std::string &message) const override
  {
    cout << m_name << " " << this << " " << message << endl;
  }
  virtual std::string read() const override
  {
    std::stringstream ss;
    ss << std::hex << this;
    auto self = ss.str();
    return m_name + " " + self + " did read something";
  }
};

class RoSocket : public RwSocket
{
  public:
   RoSocket(const std::string &name)
   : RwSocket(name)
   {}
   virtual ~RoSocket() {}
   RoSocket(RoSocket&rhs)
      : RwSocket(rhs)
   {}
   RoSocket(const RoSocket&&rhs)
      : RwSocket(std::move(rhs))
   {}

  virtual void write(const std::string &message) const override
  {
  }

  virtual std::string read() const override
  {
    return RwSocket::read();
  }

};

void work(std::vector<RwSocket> &&sockets)
{
  for(auto&& socket : sockets)
  {
    socket.write("writing outgoing message");
    cout << socket.read() << endl;
  }
}

int main(int argc, char *argv[])
{
   std::vector<RwSocket> sockets;
   sockets.emplace_back(std::move(RoSocket("ro")));
   sockets.emplace_back(std::move(RwSocket("rw")));
   work(std::move(sockets));
   return;
}

输出:

ro ctor0x7fff9752a120
ro mvtor&&0x1f8c590
ro dtor0x7fff9752a120
rw ctor0x7fff9752a180
rw mvtor&&0x1fa2bb8
ro cptor&0x1fa2b90
ro dtor0x1f8c590
rw dtor0x7fff9752a180
ro 0x1fa2b90 writing outgoing message
ro 0x1fa2b90 did read something
rw 0x1fa2bb8 writing outgoing message
rw 0x1fa2bb8 did read something
ro dtor0x1fa2b90
rw dtor0x1fa2bb8

但是当我评论其中任何一个

   sockets.emplace_back(std::move(RoSocket("ro")));
   sockets.emplace_back(std::move(RwSocket("rw")));

然后不会调用复制构造函数。

如果我多次推送,为什么会调用复制构造函数?

0 个答案:

没有答案