我有一个关于“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