c++ - 这个指针的使用

标签 c++ class stl this

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

以这种方式使用“this”通常是不好的做法吗?

感谢您的帮助。

最佳答案

不,只要您在所有权和删除方面小心谨慎,这没有任何问题。

关于c++ - 这个指针的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3096617/

相关文章:

c++ - 来自 C++ gSOAP 或 C++/CLI 的 WCF 通信?

python - 将 C++ 类指针的向量发送到 python

c++ - 删除 `end()` 的 `std::list` 的行为是什么?

c++ - 在 C++ 中解析这个的最好方法是什么?

c++ - 尽管链接了正确的库,但仍解析未解析的外部符号 CSourceSeeking::CSourceSeeking(...)

android - 在 Eclipse 中查看变量时 gdb 进程崩溃

Java 类要求

php - spl_autoload_register() 在不同目录中

c++ - 如何使用 shared_ptr 避免悬挂指针?

C++ 接口(interface)创建实例时出现问题