LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 23 of 27

Move Semantics

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Programs & Examples

Move Semantics · Study material

Counting copies and moves in matrix addition

operator+ takes its left operand by value. For x + y the parameter is a copy of x; for (x + y) + z it is the temporary result, which is moved in. The only copy is the unavoidable one.

#include <iostream>
#include <utility>
#include <vector>

class Matrix {
    std::size_t n_;
    std::vector<double> a_;
public:
    static inline int copies = 0, moves = 0;
    explicit Matrix(std::size_t n, double v = 0) : n_(n), a_(n * n, v) {}
    Matrix(const Matrix& o) : n_(o.n_), a_(o.a_) { ++copies; }
    Matrix(Matrix&& o) noexcept : n_(o.n_), a_(std::move(o.a_)) { ++moves; }
    Matrix& operator=(const Matrix& o) { n_ = o.n_; a_ = o.a_; ++copies; return *this; }
    Matrix& operator=(Matrix&& o) noexcept { n_ = o.n_; a_ = std::move(o.a_); ++moves; return *this; }
    friend Matrix operator+(Matrix a, const Matrix& b) {          // a is a copy OR a moved temporary
        for (std::size_t i = 0; i < a.a_.size(); ++i) a.a_[i] += b.a_[i];
        return a;                                                // implicit move of the parameter
    }
    double at(std::size_t i, std::size_t j) const { return a_[i * n_ + j]; }
};

int main() {
    Matrix x(300, 1), y(300, 2), z(300, 3);
    Matrix sum = x + y + z;       // (x + y) is a temporary, so the second + receives it by MOVE
    std::cout << sum.at(0, 0) << " copies=" << Matrix::copies << " moves=" << Matrix::moves << '\n';
    return 0;
}

Output

6 copies=1 moves=2

A buffer class with all five special members

The rule-of-five example shows a deep copy, a stealing move and a move assignment that frees the old buffer first.

Forwarding arguments without losing rvalue-ness

The perfect-forwarding example compares a relay that turns everything into lvalues with one that uses std::forward.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C++ Programming

C++ adds object-oriented programming, templates and the powerful Standard Template Library (STL) to C. It powers game engines, browsers, trading systems and most competitive programming. This section takes you from basic syntax to classes, inheritance, polymorphism, modern C++ (C++11 to C++23) and the STL – every concept with runnable examples and practice questions checked on a real compiler.

Why it matters

  • OOP concepts – classes, inheritance, polymorphism, encapsulation – are asked in almost every interview
  • C++ with the STL is the most popular language for competitive programming and DSA rounds
  • Questions on constructors, virtual functions, references and smart pointers decide technical rounds
  • Modern C++ skills are in demand at product companies, fintech and game studios

Who it is for

  • School students (Class 11–12 computer science with C++) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA) learning OOP and data structures for semester exams and labs
  • Freshers preparing for placements, coding tests and C++ technical interviews
  • Competitive programmers who want to master the STL

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Product company interviews
  • Coding rounds & DSA
  • Competitive programming
  • GATE CS
  • University exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links