C++为什么调用这个析构函数,它从哪里来

C++ why is this destructor called and where does it come from

我正在学习析构函数、复制和克隆,因此我编写了这段代码。除了一行之外,一切对我来说都很清楚。它是第 5 行 Destructor myClass1 0x28fec0 中的析构函数。它来自哪里,为什么叫它?

控制台输出:

Constructor myClass1 0x28fe98
Constructor myClass2 0x28fe88
Constructor myClass3 0x28fe78
operator=
Destructor myClass1 0x28fec0
clone
Constructor clone 0x28fed0
operator=
Destructor clone 0x28fed0
Destructor myClass3 0x28fe78
Destructor myClass2 0x28fe88
Destructor myClass1 0x28fe98

main.cpp:

#include <iostream>
#include "MyClass.h"

int main(){
    MyClass myClass1("myClass1"), myClass2("myClass2"), myClass3("myClass3");
    myClass2 = myClass1;
    myClass3 = myClass1.clone();
    return 0;
}

MyClass.h:

#ifndef MYCLASS_H
#define MYCLASS_H

#include <iostream>
#include <string>

class MyClass{
private:
    int *values;
    int size;
    int *copies;
    std::string name;
public:
    MyClass();
    MyClass(std::string name);
    ~MyClass();
    void operator=(MyClass myClass);
    MyClass clone();
};

#endif

MyClass.cpp:

#include "MyClass.h"

MyClass::MyClass(){
    std::cout << "Constructor" << this << std::endl;
    name = "None";
    size = 1;
    values = new int[1];
    values[0] = 0;
    copies = new int;
    *copies = 1;
    count++;
}

MyClass::MyClass(std::string name){
    std::cout << "Constructor " << name << " " << this << std::endl;
    size = 1;
    values = new int[1];
    values[0] = 0;
    copies = new int;
    *copies = 1;
    count++;
    this->name = name;
}

MyClass::~MyClass(){
    std::cout << "Destructor " << name << " " << this << std::endl;
    if(*copies == 1){
        delete [] values;
        values = 0;
        delete copies;
        copies = 0;
    }
    count--;
}

void MyClass::operator=(MyClass myClass){
    std::cout << "operator=" << std::endl;
    if(*copies == 1){
        delete copies;
        copies = 0;
        delete [] values;
        values = 0;
    }
    size = myClass.size;
    values = myClass.values;
    copies = myClass.copies;
    (*copies)++;
}

MyClass MyClass::clone(){
    std::cout << "clone" << std::endl;
    MyClass myClass("clone");
    myClass.size = size;
    delete [] myClass.values;

    myClass.values = new int[size];
    for(int i = 0; i < size; i++){
        myClass.values[i] = values[i];
    }
    return myClass;
}

因为您将 operator= 的参数作为副本。

因此它在 operator= 中创建了一个 MyClass 对象,它在 operator= returns 时被销毁。

void MyClass::operator=(MyClass myClass) 按值获取参数。所以,当你打电话给

myClass2 = myClass1;

复制构造函数将 myClass1 的副本创建到 MyClass::operator=MyClass 参数中。当该函数完成时,该副本将被销毁。要看到这种情况发生,请将打印语句放入复制构造函数 MyClass::MyClass(const MyClass&).

此外,引用 cppreference,类型 T 的赋值运算符的规范重载是

T& T::operator=(T arg) { // copy/move constructor is called to construct arg
    swap(arg);    // resources exchanged between *this and arg
    return *this;
}  // destructor is called to release the resources formerly held by *this

这是复制和交换的习惯用法。另请注意 return 类型是 T& 而不是 void.

void operator=(MyClass myClass); 应该是 MyClass& operator=(const MyClass& myClass);(标准)。否则,您通过副本传递 myClass,这意味着创建一个临时对象....然后销毁。