c++ - 我如何知道 C++ 中所有者对象的地址?

标签 c++ templates notifications one-to-many

我想在 C++ 中创建一个 Notifier 类,我将在其他对象中使用它来在对象被销毁时通知各种持有者。

template <class Owner>
class Notifier<Owner> {
public:
  Notifier(Owner* owner);
  ~Notifier(); // Notifies the owner that an object is destroyed
};

class Owner;

class Owned {
public:
  Owned(Owner* owner);
private:
  Notifier<Owner> _notifier;
};

我的观点是,由于我有一个密集而复杂的对象图,我想避免在通知程序中存储拥有对象的地址。有没有办法更改我的通知程序类,以便它可以从自己的地址和将在编译时计算的偏移量推断出拥有对象的地址?

另请注意,任何对象都可能必须通知多个“所有者”,可能来自同一类。

谢谢。

最佳答案

关于c++ - 我如何知道 C++ 中所有者对象的地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/709790/

相关文章:

c++ - 我正在尝试从文件中读取字节并且制表符被跳过

c++ - 基于模板参数的动态命名空间使用

android - AlarmManager 的更新时间 (Android)

ios - 如何按应用程序语言本地化 iOS 通知

android - 获取通知 Intent 附加功能

python - 从 C++ 库创建 Python 包

c++ - CoCreateInstance 在 Release 中工作得很好,但在调试中没有

c++ - 使用 eigen3/sparse 的稀疏特征值

php - 如何从 magento 1 模板调用 block 方法

C++ - 有没有办法使用不变语法获取当前类类型?