我有一個關於「this」用法的問題。這個指針的用法
假設我有兩個類A & B;他們的粗略輪廓如下:
class A
{
public:
...
void AddB(B* b)
{
// inserts B into the vector v
}
private:
std::vector<B*> v;
};
class B
{
public:
...
void foo(void)
{
...
// Adds itself to the queue held in A
a.AddB(this);
}
};
正在使用「這個」這種方式普遍不好的做法?
謝謝你的幫助。
我可能會建議使用'shared_ptr'或類似的東西,如果你的所有權語義不包括週期,也是非平凡的。 – Omnifarious 2010-06-22 20:04:17
同意。這裏的終身管理是棘手的部分。 – 2010-06-22 20:05:21
對'this'使用'shared_ptr'不是微不足道的。 – 2010-06-22 20:05:47