Skip to content
Minsing Log
Go back

English Edition

Object lifecycle (whether the destructor is called depends on the object's lifetime)

Programming & CS

🎯 Motivation

Do you know the difference between the two pieces of code?

  1. Code 1
Point pt2(10, 20), pt3(30, 40);
    Point pt4 = pt2 + pt3;
  1. Code 2
Point pt2(10, 20), pt3(30, 40), pt4;
pt4 = pt2 + pt3;

The second piece of code calls the destructor. I spent five hours in a Dormammu-like time loop wondering why the destructor was being called.

🎯 Conclusion

Keywords worth referencing


🌱 Explanation 1

In the first piece of code, the temporary object is destroyed after initialization is complete. This is because, according to the C++ standard, the lifetime of a temporary object is extended until initialization is complete. In the second piece of code, the temporary object is destroyed after the assignment operation, so the destructor is called.

First code

Point pt2(10, 20), pt3(30, 40);
Point pt4 = pt2 + pt3;

In this code, the pt2 + pt3 operation creates a temporary object, and this temporary object is used to directly initialize pt4. After initialization is complete, the temporary object is destroyed. Therefore, the destructor is called only after initialization is complete.

Second code

Point pt2(10, 20), pt3(30, 40), pt4;
pt4 = pt2 + pt3;

In this code, pt4 is first created using the default constructor. Then, the pt2 + pt3 operation is performed, creating a temporary object. This temporary object is assigned to pt4. After the assignment operation is complete, the temporary object is destroyed, at which point the destructor is called.

When a temporary object is destroyed

A temporary object is destroyed after the entire expression has been evaluated. In the first piece of code, the temporary object is destroyed after initialization is complete, so the destructor is called. In the second piece of code, the temporary object is destroyed after the assignment operation is complete, so the destructor is called.

In the first piece of code, the temporary object is destroyed after initialization is complete, while in the second piece of code, the temporary object is destroyed after the assignment operation is complete.


🌱Explanation 2

I will show you how to overload the + operator for adding two objects without a destructor. Here, we will implement operator overloading using a simple 2D vector class.

#include <iostream>

class Vector2D {
public:
    float x, y;

    Vector2D(float x = 0, float y = 0) : x(x), y(y) {}

    // + 연산자 오버로딩
    Vector2D operator+(const Vector2D& other) const {
        return Vector2D(this->x + other.x, this->y + other.y);
    }

    void print() const {
        std::cout << "(" << x << ", " << y << ")" << std::endl;
    }
};

int main() {
    Vector2D pt1(1.0, 2.0);
    Vector2D pt2(3.0, 4.0);

    // 두 객체를 더하는 연산
    Vector2D result = pt1 + pt2;

    result.print(); // 출력: (4.0, 6.0)

    return 0;
}

In this example, the Vector2D class overloads the + operator so that two vectors can be added. The pt1 + pt2 operation calls the operator+ method of the pt1 object, passing pt2 as an argument. As a result, a new Vector2D object is returned.

When this code is executed, (4.0, 6.0) is printed. In this way, operator overloading can be used without a destructor.

Citations: [1] https://stackoverflow.com/questions/9351166/does-overloading-operator-works-inside-the-class [2] https://www.geeksforgeeks.org/operator-overloading-cpp/ [3] https://www.programiz.com/cpp-programming/operator-overloading [4] https://cplusplus.com/forum/beginner/60378/ [5] https://cplusplus.com/forum/beginner/187/ [6] https://learn.microsoft.com/en-us/cpp/standard-library/overloading-the-output-operator-for-your-own-classes?view=msvc-170 [7] https://cplusplus.com/forum/beginner/52664/ [8] https://en.cppreference.com/w/cpp/language/operators

공개 여정 구독

글이 올라오면 이메일로 보내드릴게요.

개발, 창업, 배움, 실패와 회고를 짧고 솔직하게 기록합니다. 검색으로 스쳐가는 독자가 아니라 여정을 따라오는 팬을 만들기 위한 뉴스레터입니다.

뉴스레터는 준비 중입니다. 그동안 RSS 로 구독하거나 이메일 로 알림을 요청해 주세요.


Share this post:

Comments

Sign in with GitHub to leave a comment.


Previous Post
Linux Survival Log D-0 (Installing Ubuntu in WSL)
Next Post
Selecting the Right OS