Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) MoveConstructorExample1

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppMoveConstructorExample1/CppMoveConstructorExample1.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++

unix {
  QMAKE_CXXFLAGS += -Werror
}

 

 

 

 

 

./CppMoveConstructorExample1/main.cpp

 

#include <cassert>
#include <utility>

struct MyClass
{
  MyClass(const int x = 0) noexcept : m_x(x) {}
  MyClass(MyClass&& other) noexcept : m_x(other.m_x)
  {
    const_cast<int&>(other.m_x) = 0;
  }
  MyClass(const MyClass&) = delete;
  MyClass& operator=(const MyClass&) = delete;

  int GetX() const noexcept { return m_x; }

  private:
  ///'const int' is trivial, imaging this being a 'const Database' instead
  const int m_x;
};

int main()
{
  MyClass a(42);
  assert(a.GetX() == 42);

  //Initialize b with the internals of a,
  //leave a without its internals
  const MyClass b { std::move(a) };

  assert(a.GetX() == 0);
  assert(b.GetX() == 42);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml