据我所知,应该不惜一切代价避免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,我的代码运行得很好。
有什么建议吗?
答案 0 :(得分:1)
这不是问题的答案,所以我向Rajmund道歉,我意识到这个&#34;回答&#34;因为它可能会被投票。
我试图展示如何摆脱常见的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 Application\n + hooks up OnViewActivated callback\n + calls view's Activate" << endl;
Application application;
cout << "\nCalling the view's Activate again, in main" << endl;
application.m_view.Activate();
}