2016-02-12 84 views
0

我有一个类包含另一个类的数组。该类需要在数组中存储类的变量的总和。存储在数组中的类拥有自己的扩展逻辑来更新订单大小。C++ store类数组变量的总和

什么是确保类始终有数组中的对象的变量总是精确的总和的最佳方法?
是否有设计模式/最佳实践来确保这一点?

这两个类都在做很多其他的事情,所以有一个庞大的代码库。我可以使类,但没有在变化的环境Ⅰ-E不能创建类之间的继承关系等

class OrderManager { 
    int m_totalOrderSize; //needs to store the sum of all order sizes 
    std::vector<Order> Orders; //the array with the size variable.   
} 

class Order { 
    int size; //this size variable is updated in many ways in this class. 
    /* 
    This is one of the function that updates size. 
    How do I make sure whenever size gets updated here, the 
    OrderManager.m_totalOrderSize also gets updated accordingly. 
    */ 
    UpdateSize(); 
    ResetSize(); 
} 
+1

如果您使用任意大小的数组,你可能想使用一个标准库容器。如果你想保持准确的数量,确保你的所有数据都是私人的,并且有网守功能来访问它。 – tadman

+0

实际上IA m使用一个向量,编辑后。 – bsobaid

+1

具有大写“C”的'Class'不是有效的C++。 – crashmstr

回答