簡體   English   中英

計算班級成員被打印了多少次

[英]Count how many times class member was printed

我需要計算使用 Inspector 函數Print班級成員的次數。 構造函數應該設置類的私有元素。

#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類重載復制構造函數和復制賦值運算符。 現在您將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;
}

更新:有人提到明確刪除移動 ctor 和操作員是不行的,我明白這一點,但對我來說,不清楚我們是否應該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;
}

但這些更多的是評論者而不是問題作者。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM