哪个强制转换应与模板类参数一起使用,dynamic_cast或reinterpet_cast?
which cast should be used with a template class parameter, dynamic_cast or reinterpet_cast?
有人可以帮我吗?
据我所知,应该不惜一切代价避免reinterpet_cast,因为它很危险。然而,在我目前的情况下,它是唯一有效的演员阵容。通常我会使用 dynamic_cast,因为继承,但模板类专用化不允许我使用dynamic_cast。
在当前情况下,我的程序如下所示。
class Object {
public:
virtual ~Object() = default;
};
template<typename SenderType, typename ...ArgumentType>
class EventCallback : public Object {
public:
typedef void(SenderType::*Callback)(ArgumentType...);
EventCallback(Callback callback, SenderType *sender)
: m_callback{ callback }, m_sender{ sender } {
}
virtual ~EventCallback() = default;
void operator()(ArgumentType ...args) {
(m_sender->*m_callback)(args...);
}
private:
Callback m_callback;
SenderType *m_sender;
};
template<typename SenderType, typename ...ArgumentType>
class Event : public Object {
public:
virtual ~Event() = default;
void operator+=(EventCallback<SenderType, ArgumentType...> callback) {
m_callbacks.emplace_back(callback);
}
void operator()(ArgumentType ...args) {
for (auto callback : m_callbacks) {
callback(args...);
}
}
private:
std::vector<EventCallback<SenderType, ArgumentType...>> m_callbacks;
};
class ApplicationView : public Object {
public:
Event<Object> Activated;
void Activate() {
// activation logic ...
Activated();
}
};
class Application : public Object {
public:
Application() {
auto onViewActivated = EventCallback<Application>{&Application::OnViewActivated, this };
m_view.Activated += reinterpret_cast<EventCallback<Object>&>(onViewActivated);
m_view.Activate();
}
void OnViewActivated() {
}
};
如果我将上面的代码修改为dynamic_cast,我会得到不好dynamic_cast异常。如果我使用 reinterpet_cast,我的代码运行良好。
有什么建议吗?
这不是对所提问题的答案,所以我向Rajmund道歉,我意识到这个"答案"可能会因此而被否决。
我试图展示如何摆脱通用Object
类,并完全避免使用强制转换。 太长而无法在注释中显示,并且代码片段在注释中的格式无论如何都不好。
应该编译并运行。 我用了C++17。 我添加了一些占位符代码,这是在 Rajmund 的代码中假设的。 我添加了cout
来显示正在发生的事情。
我通常不写模板,所以我希望模板代码可以做得更好。 特别是,如果依赖代码可以执行Event<void()>
而不是Event<function<void()>>
,那就太好了......我将把它留给读者作为练习。
#include <functional>
#include <vector>
#include <iostream>
using std::function;
using std::vector;
using std::cout;
using std::endl;
template<typename F>
class Event
{
public:
void operator+=(F callback)
{
m_callbacks.emplace_back(callback);
}
template <typename ...ArgumentTypes>
void operator()(ArgumentTypes ...args)
{
// Need a copy, in case a callback adds more callbacks, or removes itself.
// (I assume callbacks removing themselves, like C#, will be added.)
auto temp_callbacks = m_callbacks;
cout << "Event callback list has " << m_callbacks.size() << endl;
for (auto callback : temp_callbacks)
{
cout << "Calling Event callback..." << endl;
callback(args...);
}
}
private:
vector<F> m_callbacks;
};
class ApplicationView final
{
public:
Event<function<void()>> Activated;
void Activate()
{
cout << "ApplicationView::Activate has just been called." << endl;
Activated();
}
};
class ApplicationViewProvider final
{
public:
static ApplicationView CreateView();
};
ApplicationView ApplicationViewProvider::CreateView()
{
return ApplicationView{};
}
class Application final
{
friend int main();
ApplicationView m_view;
public:
Application() :
m_view{ ApplicationViewProvider::CreateView() }
{
auto onViewActivated = [this]() { this->OnViewActivated(); };
m_view.Activated += onViewActivated;
m_view.Activate();
}
void OnViewActivated()
{
cout << "HURRAY! Application::OnViewActivated has been notified." << endl;
}
};
int main()
{
cout << "Making Applicationn + hooks up OnViewActivated callbackn + calls view's Activate" << endl;
Application application;
cout << "nCalling the view's Activate again, in main" << endl;
application.m_view.Activate();
}
相关文章:
- 如何理解C++标准N3337中的expr.const.cast子句8
- C++Cast运算符过载
- 在成员dynamic_bitset上使用 boost::from_block_range 时出错,但在本地dynamic
- C++类中的二维"dynamic"数组?
- 错误:"cast"未命名类型void setCastDescription(std::string
- 通过使用 const-cast 的非常量引用来延长临时的寿命
- "(void) cast"与功能有什么区别 "__attributes__"来沉默未使用的参数警告?
- protobuf in C++ with dynamic binding for google::protobuf::M
- 警告的原因是什么:"when type is in parentheses, array cannot have dynamic size"?
- C++:"Expected '(' for function-style cast or type construction"错误
- 为什么选择 g++ 给予者:"error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]"
- CUDA 错误:"dynamic initialization is not supported for __device__, __constant__ and __shared__ variabl
- Gtk+ g_signal_connect() 和 C++ lambda 会导致"invalid cast"错误
- 如何修复'The procedure entry point SDL_RWclose could not be located in the dynamic link library'
- Shared_ptr cast vs static_cast speed
- 在 iOS 上使用 Aruco 构建 OpenCV 时"Functional-style cast from id to double is not allowed"
- "The ordinal 344 could not be located in the dynamic link library"
- 覆盖 CAST 运算符(我认为它被称为向下转换)
- Dynamic Cast C++ Fail
- dynamic-cast-c++dynamic_cast错误处理