【问题标题】:Why is this part repeated three times?为什么这部分要重复三遍?
【发布时间】:2021-06-01 01:29:18
【问题描述】:

这是我的代码:

#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++中构造函数和析构函数的本质不是很了解,请高人指教!

【问题讨论】:

  • 我是学习c++的初学者。
  • 这一行car(motor m,doors d,wheels w):m(m),d(d),w(w) 为每个电机、门和轮子调用两个复制构造函数。您没有自定义复制构造函数,因此它不会打印“我正在创建”消息。每个项目在传递给构造函数时被复制一次(按值传递),并在创建成员对象时被复制一次。有关更多信息,请咨询您的教学人员。
  • 帮助你日后提问:你问为什么“这部分”重复了三遍,却没有说是哪部分。
  • 对不起,这部分:这辆车的马达被拆了。这辆车的门被拆除了。这辆车的轮子被拆掉了。

标签: c++ constructor destructor


【解决方案1】:

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

#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 被销毁

希望这能清除您的理解。

【讨论】:

    【解决方案2】:
    motor m;
    doors d;
    wheels w;
    

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

    我是这辆车的马达。
    我是这辆车的门。
    我是车轮 这辆车。

    car(m, d, w);
    

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

    这辆车已组装好
    这辆车的电机已拆下。
    这辆车被拆除了。
    这辆车的轮子被拆除了。

        return 0;
    }
    

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

    这辆车被拆除了。
    这辆车的轮子被拆除了。
    这辆车被拆除了。
    这辆车的马达被拆除了。

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

    这辆车的轮子被拆掉了。
    这辆车的车门被拆除了。
    这辆车的马达被拆掉了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-06
      • 2012-10-10
      • 2019-01-28
      • 2011-03-01
      • 2010-12-15
      • 1970-01-01
      相关资源
      最近更新 更多