继承的 class 中的 shared_from_this() 类型错误(是否有 dyn.type-aware 共享指针?)
Wrong type in shared_from_this() in inherited class (is there a dyn.type-aware shared pointer?)
我有一个使用 enable_shared_from_this<>
的基本视图控制器 class
class ViewController :
public std::enable_shared_from_this<ViewController>
{ // ...
};
和一个child:
class GalleryViewController : public ViewController {
void updateGallery(float delta);
}
问题出现了,当我尝试将我当前的实例传递给第 3 方(比如 lambda 函数被安排在某处)
实例 (GalleryViewController
) 将解除分配的(罕见)条件,因此我无法直接捕获 'this',我需要捕获具有 shared_from_this()
的共享组:
void GalleryViewController::startUpdate()
{
auto updateFunction = [self = shared_from_this()](float delta)
{
return self->updateGallery(delta); // ERROR: ViewController don't have updateGallery() method!
};
scheduler->schedule(updateFunction); // takes lambda by value
}
问题是 shared_from_this()
returns 没有 updateGallery()
方法的 shared_ptr<ViewController>
。
我真的很讨厌 dynamic_cast
(在这种情况下甚至是静态的),这是维护的噩梦。而且代码很丑!
updateFunction = [self = shared_from_this()](float delta)
{
auto self2 = self.get();
auto self3 = (UIGalleryViewController*)self2;
return self3->updateGallery(delta);
};
有什么默认模式可以解决这个问题吗? dynamic-type 知道共享指针吗?我应该用 enable_shared_from_this<GalleryViewController>
双重继承 child class 吗?
void GalleryViewController::startUpdate(bool shouldStart)
{
if (shouldStart == false) {
updateFunction = [self = shared_from_this()](float delta)
{
return self->updateGallery(delta); // ERROR: ViewController don't have updateGallery() method!
};
scheduler->schedule(updateFunction); // takes lambda by value
}
The problem is that shared_from_this()
returns a
shared_ptr<ViewController>
that doesn't have the updateGallery()
method.
I really hate to do dynamic_cast (or even static in this case) its the
maintenance nightmare. And the code is ugly!
这就是 std::static_pointer_cast
and std::dynamic_pointer_cast
的用途。您不必在转换前使用 .get()
获取原始指针。
void GalleryViewController::startUpdate(bool shouldStart)
{
if (shouldStart == false) {
updateFunction = [self = std::static_pointer_cast<GalleryViewController>(shared_from_this())](float delta)
{
return self->updateGallery(delta);
};
scheduler->schedule(updateFunction); // takes lambda by value
}
我有一个使用 enable_shared_from_this<>
class ViewController :
public std::enable_shared_from_this<ViewController>
{ // ...
};
和一个child:
class GalleryViewController : public ViewController {
void updateGallery(float delta);
}
问题出现了,当我尝试将我当前的实例传递给第 3 方(比如 lambda 函数被安排在某处)
实例 (GalleryViewController
) 将解除分配的(罕见)条件,因此我无法直接捕获 'this',我需要捕获具有 shared_from_this()
的共享组:
void GalleryViewController::startUpdate()
{
auto updateFunction = [self = shared_from_this()](float delta)
{
return self->updateGallery(delta); // ERROR: ViewController don't have updateGallery() method!
};
scheduler->schedule(updateFunction); // takes lambda by value
}
问题是 shared_from_this()
returns 没有 updateGallery()
方法的 shared_ptr<ViewController>
。
我真的很讨厌 dynamic_cast
(在这种情况下甚至是静态的),这是维护的噩梦。而且代码很丑!
updateFunction = [self = shared_from_this()](float delta)
{
auto self2 = self.get();
auto self3 = (UIGalleryViewController*)self2;
return self3->updateGallery(delta);
};
有什么默认模式可以解决这个问题吗? dynamic-type 知道共享指针吗?我应该用 enable_shared_from_this<GalleryViewController>
双重继承 child class 吗?
void GalleryViewController::startUpdate(bool shouldStart) { if (shouldStart == false) { updateFunction = [self = shared_from_this()](float delta) { return self->updateGallery(delta); // ERROR: ViewController don't have updateGallery() method! }; scheduler->schedule(updateFunction); // takes lambda by value }
The problem is that
shared_from_this()
returns ashared_ptr<ViewController>
that doesn't have theupdateGallery()
method.I really hate to do dynamic_cast (or even static in this case) its the maintenance nightmare. And the code is ugly!
这就是 std::static_pointer_cast
and std::dynamic_pointer_cast
的用途。您不必在转换前使用 .get()
获取原始指针。
void GalleryViewController::startUpdate(bool shouldStart)
{
if (shouldStart == false) {
updateFunction = [self = std::static_pointer_cast<GalleryViewController>(shared_from_this())](float delta)
{
return self->updateGallery(delta);
};
scheduler->schedule(updateFunction); // takes lambda by value
}