无法理解为什么复制构造函数被调用

问题描述 投票:0回答:1

以下是正在执行的所有代码:

void test2(){
    Vector p1_p(5, 0, 0);
    Vector p1_v(1, 2, 0);
    Vector p1_a;
    Vector p1_f;
    double p1_d = 0;
    double p1_m = 1;

    std::cout << "Before Particle" << std::endl;
    Particle p1(p1_p, p1_v, p1_a, p1_f, p1_d, p1_m);

    std::cout << "Particle 1 initial position is: " << p1.position << std::endl;
}

以下是Vector类的部分实现

Vector::Vector() : x(0), y(0), z(0) {
    std::cout << "Default constructing Vector: (" << x << ", " << y << ", " << z << ")" << std::endl;
};
// constr
Vector::Vector(const double x, const double y, const double z) : x(x), y(y), z(z) {
    std::cout << "Constructing Vector with: (" << x << ", " << y << ", " << z << ")" << std::endl;
};
// destr
Vector::~Vector() {};
// cpy constr
Vector::Vector(const Vector &other) : x(other.x), y(other.y), z(other.z) {
    std::cout << "Copy constructing Vector: (" << x << ", " << y << ", " << z << ")" << std::endl;
};
// assign
Vector& Vector::operator=(const Vector &other) {
    if (this != &other) {
        x = other.x;
        y = other.y;
        z = other.z;
    }
    std::cout << "Assigning Vector: (" << x << ", " << y << ", " << z << ")" << std::endl;
    return *this;
};

以及 Particle 类实现的最后一部分

Particle::Particle(const Vector &p, const Vector &v, const Vector &a, const Vector &f, double d, double m)
    : position(p), velocity(v), acceleration(a), appliedForces(f), damping(d), inverseMass(m) {
    std::cout << "Constructing Particle with position: " << position << std::endl;
}


Particle::~Particle() {
    // TODO Auto-generated destructor stub
}

Particle::Particle(const Particle &other)
    : position(other.position), velocity(other.velocity), acceleration(other.acceleration),
      appliedForces(other.appliedForces), damping(other.damping), inverseMass(other.inverseMass) {
    std::cout << "Copy constructing Particle with position: " << position << std::endl;
}

Particle& Particle::operator=(const Particle &other) {
    if (this != &other) {
        position = other.position;
        velocity = other.velocity;
        acceleration = other.acceleration;
        appliedForces = other.appliedForces;
        damping = other.damping;
        inverseMass = other.inverseMass;
    }
    std::cout << "Assigning Particle with position: " << position << std::endl;
    return *this;
}

最后是控制台输出。由于在 test2 开头声明的每个向量的复制构造函数在调用 Particle 构造函数的确切时间被调用,所以发生了什么?因为我将向量作为 const 引用传递,所以这不应该发生吗?

  • 启动
  • 用 (5, 0, 0) 构造向量
  • 用 (1, 2, 0) 构造向量
  • 默认构造向量:(0, 0, 0)
  • 默认构造向量:(0, 0, 0)
  • 粒子之前
  • 复制构造向量:(5,0,0)
  • 复制构造向量:(1, 2, 0)
  • 复制构造向量:(0, 0, 0)
  • 复制构造向量:(0, 0, 0)
  • 构造位置为:(5,0,0)的粒子
  • 粒子 1 初始位置为:(5, 0, 0)
  • 终止

我不明白为什么要调用复制构造函数。我确保将每个 Vector 作为 const 引用传递。

c++ constructor reference copy constants
1个回答
-1
投票

在此构造函数的 mem-initializer 列表中

Particle::Particle(const Vector &p, const Vector &v, const Vector &a, const Vector &f, double d, double m)
    : position(p), velocity(v), acceleration(a), appliedForces(f), damping(d), inverseMass(m) {
    std::cout << "Constructing Particle with position: " << position << std::endl;
}

有显式调用类的复制构造函数

Vector
例如

position(p)
© www.soinside.com 2019 - 2024. All rights reserved.