简体   繁体   English

如何在不使用c ++中的全局变量的情况下存储对象的状态

[英]How to store the state of an object without using global variables in c++

I'm new to C++ and while learning i came across a lot of places which say that using global variables should be avoided and used in very rare cases. 我是C ++的新手,在学习的过程中,我遇到了许多地方,他们认为应该避免使用全局变量并在极少数情况下使用。

My question is, How can i store the state of an object so then it can be accessed at a later time without using global variables? 我的问题是,如何存储对象的状态,以便以后可以在不使用全局变量的情况下访问它? Here's an example: 这是一个例子:

A Circle class may have its state stored by using diameter, x and y as globals. Circle类可以通过使用直径,x和y作为全局来存储其状态。 Then these values can be modified and retrieved with methods. 然后可以使用方法修改和检索这些值。

Is there a way to accomplish the above without using globals that is preferred? 有没有办法在不使用首选的全局变量的情况下完成上述操作?

Thanks 谢谢

Edit: I had globals confused with member variables and that was the problem. 编辑:我的全局变量与成员变量混淆,这就是问题所在。 Now i feel really stupid for asking this question but hopefully this will help someone else. 现在我对提出这个问题感到非常愚蠢,但希望这会有助于其他人。

You normally store an object's state using member variables. 您通常使用成员变量存储对象的状态。

class Circle
{
   ...
private:
   double x, y;
   double diameter;
};

Wanting to store an object's state in global variables is really bizarre because then you could only have one object. 想要将对象的状态存储在全局变量中是非常奇怪的,因为那时你只能有一个对象。

If you use globals to store the state of the Circle, then you can only have one Circle at any time, because if you had two, they would both be trying to use the same global variables to store their state, and that would be messy. 如果你使用全局变量来存储Circle的状态,那么你任何时候都只能有一个Circle,因为如果你有两个,他们都会尝试使用相同的全局变量来存储它们的状态,这将是凌乱的。

Make state variables member variables, so that each class instance has its own version of them. 使状态变量成员变量,以便每个类实例都有自己的版本。 This is one of the principles of object oriented programming. 这是面向对象编程的原则之一。

If you have variables that need to be shared between all instances of the class , make them static member variables . 如果您需要在类的所有实例之间共享变量,请将它们设为静态成员变量 That way you know only your objects can access them (if you choose to use private or protected visibility on them) and no one else who doesn't know what they're doing will come in a screw with them. 这样你就知道只有你的对象可以访问它们(如果你选择使用它们的privateprotected可见性),并且没有其他人不知道他们正在做什么将与他们搞混。

Example: 例:

class Circle {
public:
    Circle(double x, double y, double diameter) : x(x), y(y), diameter(diameter) { }
    double X() const { return x; }
    double Y() const { return y; }
    double diameter() const { return diameter; }

    ...

private:
    double x, y, diameter;
};

Now when we have two Circle s, they behave fine: 现在,当我们有两个Circle ,它们表现得很好:

Circle c1(4.0, 4.0, 6.0);
Circle c2(1.0, 6.0, 3.0);
cout << c1.X() << endl
     << c2.X() << endl;
// prints 4 and 1

But if we do it this way, with globals: 但如果我们这样做,使用全局变量:

// BAD, DON'T DO THIS
double x = 0, y = 0, diameter = 0;

class Circle {
public:
    Circle(double _x, double _y, double _diameter) {
        x = _x, y = _y, diameter = _diameter;
    }

    double X() const { return x; }
    double Y() const { return y; }
    double diameter() const { return diameter; }
};

When we have two circles: 当我们有两个圈子时:

Circle c1(4.0, 4.0, 6.0);
Circle c2(1.0, 6.0, 3.0);
cout << c1.X() << endl
     << c2.X() << endl;
// prints 1 and 1

The last Circle you create will overwrite all the other Circle s' data. 您创建的最后一个Circle将覆盖所有其他Circle的数据。 As you can see, that's not a good thing. 如你所见,这不是一件好事。

There are three kind of variables in C++: C ++中有三种变量:

  • Global variables 全局变量
  • Member variables 成员变量
  • Local variables 局部变量

An example might help: 一个例子可能有帮助:

const float pi = 3.14; // A global variable. Always the same value.

class Circle
{
  private:
    float radius; // Member variable.

  public:
    Circle(float radius=0) { this.radius = radius; }
    float GetRadius() { return radius; }
    float GetDiameter() { return radius * 2; }
    float GetArea() { return pi * radius * radius; }
}

void PrintCircle(Circle& c, char* name)
{
  cout << name << " Radius: " << c.GetRadius() << ", Diameter: " << c.GetDiameter()
    << ", Area: " << c.GetArea();
}

void ShowCircle(float radius)
{
  Circle unit(1);  // Local variable
  Circle c(radius);  // Local variable
  PrintCircle(unit, "Unit circle");
  PrintCircle(c, "My circle");
}

int main(int argc, int** argv)
{
  ShowCircle(2);
  ShowCirlce(3);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM