[C++/C] How to Batch Copy (Assign) All Struct Members with =

目次

Introduction

In C++ and C structures (struct), there are cases where you want to copy the contents of one variable exactly into another variable. While it is possible to copy members one by one like destination.id = source.id;, this becomes very tedious if there are many members.

Fortunately, in C++ and C, if the structure variables are of the same type, you can use the assignment operator (=) to copy all members in a single operation.

In this article, I will explain the basic usage of assignment (batch copying) between structure variables, along with sample code.

Sample Code for Batch Copying Structures

This code defines a Book structure, sets information in a variable called originalBook, and then uses = to batch copy that content into another variable called copiedBook.

Complete Code

#include <iostream>
#include <string>

// Definition of "Book" structure
struct Book {
    std::string title; // Title
    std::string author; // Author
    int price;         // Price
};

int main() {
    // 1. Prepare the source structure variable
    Book originalBook;
    originalBook.title = "Introduction to C++ Programming";
    originalBook.author = "Taro Yamada";
    originalBook.price = 2800;

    // 2. Declare the destination structure variable
    Book copiedBook;

    // 3. Batch copy all members using the assignment operator (=)
    copiedBook = originalBook;

    // 4. Check the copied contents
    std::cout << "Copied Book Information" << std::endl;
    std::cout << "Title: " << copiedBook.title << std::endl;
    std::cout << "Author: " << copiedBook.author << std::endl;
    std::cout << "Price: " << copiedBook.price << " yen" << std::endl;

    return 0;
}

Execution Result

Copied Book Information
Title: Introduction to C++ Programming
Author: Taro Yamada
Price: 2800 yen

Code Explanation

copiedBook = originalBook;

This single line is the core of this article.

The values of all members (title, author, price) stored in originalBook are automatically copied one by one to the corresponding members of copiedBook.

Internally, this is equivalent to the compiler automatically performing the following processing:

// copiedBook = originalBook; is equivalent to the following:
copiedBook.title = originalBook.title;
copiedBook.author = originalBook.author;
copiedBook.price = originalBook.price;

This mechanism, known as “member-wise copy,” allows you to write code very concisely.

Important Caution

This simple copy works perfectly well when the structure’s members consist of basic data types like int or std::string.

However, you must be careful when dealing with structures that have pointers as members. Copying with = copies the pointer’s address value itself, not the actual data pointed to in memory (this is called a Shallow Copy). This causes two structures to point to the same memory area, which can lead to unexpected problems. When dealing with pointers, you may need to implement a “Deep Copy” yourself if necessary.

Summary

In this article, I explained how to batch copy the contents of a structure to another structure variable using the assignment operator =.

  • Structure variables of the same type can have all member values copied using =.
  • This is called a member-wise copy.
  • If you have pointers as members, be careful of the behavior (Shallow Copy).

There is no need to write redundant code to copy members one by one; you can smartly duplicate structures just by using =.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次