开发者

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.

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. 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.

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.

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 Circles, they behave fine:

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 Circles' data. As you can see, that's not a good thing.


There are three kind of variables in 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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜