C++ - Standard library - move

The move template function allows to change an object owner.

We're not going to dive in the heart of the move semantics.

Instead we're going to see a really easy example of how it works.

Let's get started.

First of all

In this move tutorial we're going to create 3 Ships, check their memory address then pass them to a Manager in order to add them to the Manager list.

And each time, we change the scope from a method to another one, the current owner of a ship will transfer the object to another one in a new method or a list.

So the memory address, of the current owner, will be lost after each transfer to the new owner.

The last owner will be the Manager list.

Code

main.cpp

#include <iostream>
#include <string>
#include <list>
#include <memory>

// Badprog.com

class Ship;

// Manager
class Manager {

public:
  /**
   * CTOR
   * */
  Manager() {
    std::cout << __FUNCTION__ << std::endl;
  }

  /**
   * DTOR
   * */
  virtual ~Manager() {
    std::cout << __FUNCTION__ << std::endl;
  }

  /**
   * addToList
   * */
  void addToList(std::unique_ptr<Ship> ship) {
    std::cout << "ship " << ship.get() << std::endl;
    _listShip.push_back(std::move(ship));
    std::cout << "ship " << ship.get() << std::endl;
  }

  /**
   * displayWhatThereIsInTheList
   * */
  void displayWhatThereIsInTheList() {

    std::list<std::unique_ptr<Ship>>::iterator iterator;
    iterator = _listShip.begin();

    while (iterator != _listShip.end()) {
      std::cout << "ship: " << iterator->get() << std::endl;
      ++iterator;
    }
  }

private:
  std::list<std::unique_ptr<Ship>> _listShip;

};

// Ship
class Ship {
public:

  /**
   * CTOR
   * */
  Ship(const std::string& name) : _name(name) {
    std::cout << __FUNCTION__ << " with name " << _name << std::endl;
  }

  /**
   * DTOR
   * */
  virtual ~Ship() {
    std::cout << __FUNCTION__ << " byebye I was the " << _name << std::endl;
  }
private:
  std::string _name;
};

/**
* Main
* */
int main() {
  auto ship1  = std::make_unique<Ship>("ShipTiny");
  auto ship2  = std::make_unique<Ship>("ShipNormal");
  auto ship3  = std::make_unique<Ship>("ShipEnormous");

  Manager manager;

  std::cout << "ship1 " << ship1.get() << std::endl;
  std::cout << "ship2 " << ship2.get() << std::endl;
  std::cout << "ship3 " << ship3.get() << std::endl;

  manager.addToList(std::move(ship1));
  manager.addToList(std::move(ship2));
  manager.addToList(std::move(ship3));

  std::cout << "ship1 " << ship1.get() << std::endl;
  std::cout << "ship2 " << ship2.get() << std::endl;
  std::cout << "ship3 " << ship3.get() << std::endl;

  manager.displayWhatThereIsInTheList();
}

 

Compiling, linking and running

g++ main.cpp -o go.exe -std=c++1y ; ./go.exe

Result

Ship::Ship with name ShipTiny

Ship::Ship with name ShipNormal

Ship::Ship with name ShipEnormous

Manager::Manager

ship1 02FF9988

ship2 02FF4A70

ship3 02FFED40

ship 02FF9988

ship 00000000

ship 02FF4A70

ship 00000000

ship 02FFED40

ship 00000000

ship1 00000000

ship2 00000000

ship3 00000000

ship: 02FF9988

ship: 02FF4A70

ship: 02FFED40

Manager::~Manager

Ship::~Ship byebye I was the ShipTiny

Ship::~Ship byebye I was the ShipNormal

Ship::~Ship byebye I was the ShipEnormous

Conclusion

As expected, each time the owner changes, we can see the memory address transforms to zero.

It isn't just a copy but a complete transfer.

Well done, you got it. cool

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.