我有两个类,“缓存”和“LRU”: 类缓存看起来像这样:
class cache
{
private:
int num_cold; //Number of cold misses
int num_cap; //Number of capacity misses
int num_conf; //Number of conflict misses
int miss; //Number of cache misses
int hits; //Number of cache hits
public:
// methods
}
我还有一个LRU类的方法
bool LRU::access (Block block)
{
for (i = lru.begin(); i != lru.end(); i++) //If
{
if (i->get_tag() == block.get_tag() && i->get_index() == block.getIndex())
{
lru.push_back(block);
lru.erase(i);
return true;
//Here i want to add 1 to the value of variable "hits" of class "cache"
}
}
}
我想在方法“LRU::access”中增加类“cache”中变量的值。 有人可以告诉我我该怎么做吗? 谢谢。
最佳答案
将此添加到缓存
:
friend class LRU;
这将允许 LRU
中的任何代码访问 cache
的所有私有(private)成员。
关于c++:如何从另一个类的函数访问一个类的私有(private)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17367345/