为什么这部分重复了三遍?

Why is this part repeated three times?

这是我的代码:

#include  <iostream>
using namespace std;

class motor
{
public:
    motor();
    ~motor();

private:

};

motor::motor()
{
    cout << "I'm the motor of this car." << endl;
}

motor::~motor()
{
    cout << "the motor of this car is removed." << endl;
}

class doors
{
public:
    doors();
    ~doors();

private:

};

doors::doors()
{
    cout << "I'm the doors of this car." << endl;
}

doors::~doors()
{
    cout << "the doors of this car are removed." << endl;
}

class wheels
{
public:
    wheels();
    ~wheels();

private:

};

wheels::wheels()
{
    cout << "I'm the wheels of this car." << endl;
}

wheels::~wheels()
{
    cout << "the wheels of this car are removed." << endl;
}

class car
{
public:
    car(motor m,doors d,wheels w):m(m),d(d),w(w) {
        cout << "This car is assembled" << endl;
    }
    //car() {
    //  cout << "This car is assembled" << endl;
    //}
    ~car() {
        cout << "This car is removed." << endl;
    }

private:
    motor m;doors d;
    wheels w;
};


int main() {
    motor m;
    doors d;
    wheels w;
    car(m, d, w);
    //car();
    return 0;
}

结果如下:

**我是这辆车的马达

我是这辆车的车门

我是这辆车的轮子

这辆车组装好了

这辆车的电机被拆掉了

这辆车的车门被拆除了。

这辆车的轮子被拆掉了。

这辆车被删除了。

这辆车的轮子被拆掉了。

这辆车的车门被拆除了。

这辆车的电机被拆掉了

这辆车的轮子被拆掉了。

这辆车的车门被拆除了。

这辆车的电机拆了**

我对c++中构造函数和析构函数的本质还不是很了解,请高人指教!

motor m;
doors d;
wheels w;

电机、门和轮子的默认构造函数被调用。

I'm the motor of this car.
I'm the doors of this car.
I'm the wheels of this car.

car(m, d, w);

这是在做 :m(m),d(d),w(w),它制作了 3 个参数副本(使用默认的复制构造函数,您不会为此输出任何内容),然后销毁这些副本。

This car is assembled
the motor of this car is removed.
the doors of this car are removed.
the wheels of this car are removed.

    return 0;
}

汽车析构函数在主函数结束时执行。这使得汽车的私有成员也被销毁。

This car is removed.
the wheels of this car are removed.
the doors of this car are removed.
the motor of this car is removed.

电机、门和车轮的析构函数也是如此。

the wheels of this car are removed.
the doors of this car are removed.
the motor of this car is removed.

您错过了复制构造函数,因此您遇到了问题。

#include  <iostream>
using namespace std;

class motor
{
public:
    motor();
    motor(const motor& c);
    ~motor();

private:

};

motor::motor()
{
    cout << "A new motor was made" << endl;
}
motor::motor(const motor& c)
{
    cout << "A motor was cloned" << endl;
}

motor::~motor()
{
    cout << "A motor destroyed" << endl;
}


class wheels
{
public:
    wheels();
    wheels(const wheels& c);
    ~wheels();

private:

};

wheels::wheels()
{
    cout << "A new wheel was made" << endl;
}
wheels::wheels(const wheels& c)
{
    cout << "A wheel was cloned" << endl;
}

wheels::~wheels()
{
    cout << "A wheel destroyed" << endl;
}

class car
{
public:
    car(motor m2,wheels w2):m3(m2),w3(w2) {
        cout << "This car is assembled" << endl;
    }
    ~car() {
        cout << "This car is destroyed." << endl;
    }

private:
    motor  m3;
    wheels w3;
};


int main() {
    motor m1;
    wheels w1;
    car(m1, w1);
    return 0;
}

这会产生以下输出

A new motor was made
A new wheel was made
A motor was cloned
A wheel was cloned
A motor was cloned
A wheel was cloned
This car is assembled
A wheel destroyed
A motor destroyed
This car is destroyed.
A wheel destroyed
A motor destroyed
A wheel destroyed
A motor destroyed

让我们理解输出

  • 创建了 m1 和 w1
  • w1被复制到w2,m1被复制到m2(这里m2,w2是参数变量)。
  • m2被复制到m3,w2被复制到w3(顺序取决于m3和w3的声明顺序)
  • 创建了一个临时汽车对象
  • w2和m2被摧毁
  • 汽车对象被销毁
  • m3和w3被销毁
  • m1和w1被销毁

希望这能澄清您的理解。