Move Assignment Operator

Medium Move Semantics
Solve in Playground →

Problem Statement

Write a C++ program implementing a custom `Buffer` class with a move assignment operator.

Input Format

A single word string S.

Output Format

Print assigned buffer string content.

Constraints

1 <= Length <= 50

Sample Input

MoveAssignment

Sample Output

MoveAssignment

Explanation

Move assignment releases existing resources and steals pointers from temporary rvalues efficiently.

Starter Code

#include <iostream>
#include <cstring>
#include <string>

class Buffer {
private:
    char *data;

public:
    Buffer(const char *str) {
        data = new char[std::strlen(str) + 1];
        std::strcpy(data, str);
    }

    Buffer& operator=(Buffer &&other) noexcept {
        // Write your move assignment operator logic here
        return *this;
    }

    ~Buffer() {
        delete[] data;
    }

    const char* c_str() const { return data ? data : ""; }
};

int main() {
    std::string s;
    if (std::cin >> s) {
        Buffer b1(s.c_str());
        Buffer b2("Default");
        b2 = std::move(b1);
        std::cout << b2.c_str() << "n";
    }
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used