计算 class 成员被打印了多少次

Count how many times class member was printed

我需要计算 class 成员使用函数 Print 打印了多少次 inspector。构造函数应设置 class.

的私有元素
#include <cmath>
#include <iostream>
class Vector3d {
  double x, y, z;
  mutable int count = 0;
  public:
    Vector3d();
  Vector3d(double x, double y, double z);
  void Print() const;
  int GetCount() const;
};
Vector3d::Vector3d() {
  count = 0;
}
Vector3d::Vector3d(double x, double y, double z) {
  count = 0;
  Vector3d::x = x;
  Vector3d::y = y;
  Vector3d::z = z;
}
void Vector3d::Print() const {
  count++;
  std::cout << "{" << x << "," << y << "," << z << "}";
}
int Vector3d::GetCount() const {
  return count;
}
int main() {
  Vector3d v1(1, 2, 3);
  v1.Print();v1.Print();v1.Print();
  Vector3d v2(v1);
  v2.Print();v2.Print();
  std::cout << v2.GetCount();
  return 0;
}

我使用 mutable int 来启用 const 函数的更改元素。对于 v1.GetCount(),我得到输出 3,这是正确的。但是,对于 v2.GetCount() 我得到输出 5,这是错误的(正确的是 2)。

你能帮我解决这个问题吗?我哪里出错了?

您需要为 Vector3d class 重载复制构造函数和复制赋值运算符。 现在您正在将 count 字段的状态复制到 v2 对象中,因此它从 3 而不是 0 开始。

#include <cmath>
#include <iostream>
class Vector3d {
    double x, y, z;
    mutable int count = 0;
public:
    Vector3d(double x, double y, double z);
    Vector3d(const Vector3d&);
    Vector3d& operator=(const Vector3d&);
    Vector3d(Vector3d&&) = delete;
    Vector3d& operator=(Vector3d&&) = delete;
    void Print() const;
    int GetCount() const;
};
Vector3d::Vector3d(double x, double y, double z) {
    Vector3d::x = x;
    Vector3d::y = y;
    Vector3d::z = z;
}
Vector3d::Vector3d(const Vector3d& that)
: Vector3d(that.x, that.y, that.z)
{
}
Vector3d& Vector3d::operator=(const Vector3d& that)
{
    x = that.x;
    y = that.y;
    z = that.z;
    return *this;
}

void Vector3d::Print() const {
    count++;
    std::cout << "{" << x << "," << y << "," << z << "}";
}

int Vector3d::GetCount() const {
    return count;
}

int main() {
    Vector3d v1(1, 2, 3);
    v1.Print();v1.Print();v1.Print();
    Vector3d v2(v1);
    v2.Print();v2.Print();
    std::cout << v2.GetCount();
    return 0;
}

更新: 有人提到显式删除 move ctor 和 operator 是不行的,我理解,但对我来说不清楚我们是否应该 move 反对其他实例。因此这里可能的实现方式:

#include <cmath>
#include <iostream>

class Vector3d {
    double x, y, z;
    mutable int count = 0;
public:
    Vector3d(double x, double y, double z);
    Vector3d(const Vector3d&);
    Vector3d(Vector3d&&);
    Vector3d& operator=(Vector3d);
    void Print() const;
    int GetCount() const;

private:
    void swap(Vector3d&);
};
Vector3d::Vector3d(double x, double y, double z) {
    Vector3d::x = x;
    Vector3d::y = y;
    Vector3d::z = z;
}
Vector3d::Vector3d(const Vector3d& that)
        : Vector3d(that.x, that.y, that.z)
{
}
Vector3d::Vector3d(Vector3d&& that)
: Vector3d(that.x, that.y, that.z)
{
    count = that.count;
}
Vector3d& Vector3d::operator=(Vector3d that)
{
    swap(that);
    return *this;
}

void Vector3d::swap(Vector3d& that)
{
    std::swap(x, that.x);
    std::swap(y, that.y);
    std::swap(z, that.z);
    std::swap(count, that.count);
}

void Vector3d::Print() const {
    count++;
    std::cout << "{" << x << "," << y << "," << z << "}";
}

int Vector3d::GetCount() const {
    return count;
}

int main() {
    Vector3d v1(1, 2, 3);
    v1.Print();v1.Print();v1.Print();
    Vector3d v2 = std::move(v1);
    v2.Print();v2.Print();
    std::cout << v2.GetCount();
    return 0;
}

但是这些更多的是针对评论者而不是问题作者。