Home > Mobile >  Count how many times class member was printed
Count how many times class member was printed

Time:05-26

I need to count how many times class members were printed using function Print which is inspector. Constructor should set private elements of 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;
}

I used mutable int to enable changing element of const function. For v1.GetCount() I get output 3, which is correct. However, for v2.GetCount() I get output 5, which is wrong (correct is 2).

Could you help me to fix this? Where am I making mistake?

CodePudding user response:

You need to overload copy constructor and copy assign operator for Vector3d class. Now you are copying state of count field into v2 object, therefore it starts from 3 not from 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;
}

UPDATE: Someone mentioned that explicitly deleted move ctor and operator is not ok, I understand that, but for me it is not clear should we move counter to other instance or not. Therefore here possible implementation:

#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;
}

But these more for commenters than for question author.

  • Related