2013-06-28 38 views
0

我有兩個班,「緩存」和「LRU」: 級緩存看起來是這樣的:C++:如何從另一個類的函數訪問類的私有變量

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」中類「緩存」中變量的值。 有人能告訴我我該怎麼做。 謝謝。

+0

最簡單的答案是公共設置功能hit..or public increment_hit函數 – sethi

+0

LRU如何訪問緩存對象? –

回答

4

一下添加到cache

friend class LRU; 

這將讓任何代碼LRU訪問cache所有私有成員。

0

您可以將LRU聲明爲朋友類來緩存。

相關問題