說我有這個類叫做Dog。每隻狗都有不同的名字,但有相同的叫聲(從資源文件加載)。一個對象的共享資源
class Dog {
public:
Dog(const string &name) : _name(name) {
_barkingVoice.load();
}
~Dog() {
_barkingVoice.free();
}
string getName() const { return _name; }
void bark() { _barkingVoice.play(); }
private:
string _name;
VoiceResource _barkingVoice;
};
我想打電話給_barkingVoice.load()
只有狗的實例是第一位的,只有_barkingVoice.free()
如果有狗沒有更多的實例。
顯而易見的解決方案是將_barkingVoice設置爲靜態,並將Dog的引用計數器作爲數據成員。
我的問題是如果有一個更簡單的方法來做到這一點。也許是std實現或類似的東西。
太棒了。謝謝! – Pilpel