请教c++中delegate的用法相关的问题

3m2u 2017-11-13 04:38:53
问下c++ delegate的问题 还有虚函数相关的问题。看下面这个类
class Delegate 下的 virtual void OnContextInitialized(CefRefPtr<ClientAppBrowser> app) {}
1. 这个只能是虚函数吗,这个函数调用 的时候 是调用 ClientHandler类的OnContextInitialized
还是它的某个父类的 OnContextInitialized?
2. 假如不只一个父类中有这个函数会有什么后果?
3. 为什么有的函数后面是=0 有的是 virtual void OnSetFavicon(CefRefPtr<CefImage> image) {}
有什么区别么?

4. 某个代理里的函数实现如下:

void ClientAppBrowser::OnContextInitialized()
{
CefRefPtr<CefCookieManager> manager = CefCookieManager::GetGlobalManager(NULL);
manager->SetSupportedSchemes(cookie_schemes, NULL);

// 删除保存的Cooies信息
// manager->DeleteCookies(L"", L"", nullptr);

DelegateSet::iterator it = m_delegates.begin();
for (; it != m_delegates.end(); ++it)
(*it)->OnContextInitialized(this);
}

这个代理的意思是什么呢? 那么可以在ClientAppBrowser 注册多个delegateset, 在调用的时候
OnContextInitialized发现有几个代理 依次执行代理的功能,代理的这个功能是调用ClientAppBrowser的父分类的OnContextInitialized? 相当于ClientAppBrowser 可以比较方便地再去扩展OnContextInitialized 这个功能?


namespace Browser
{
class ClientApp : public CefApp
{
public:
ClientApp(){}

protected:
std::vector<CefString> cookie_schemes;
DISALLOW_COPY_AND_ASSIGN(ClientApp);
};

class ClientAppBrowser : public ClientApp , public CefBrowserProcessHandler
{
public:
class Delegate : public virtual CefBase {
public:
virtual void OnBeforeCommandLineProcessing(
CefRefPtr<ClientAppBrowser> app,
CefRefPtr<CefCommandLine> command_line) {}

virtual void OnContextInitialized(CefRefPtr<ClientAppBrowser> app) {}

virtual void OnBeforeChildProcessLaunch(
CefRefPtr<ClientAppBrowser> app,
CefRefPtr<CefCommandLine> command_line) {}

virtual void OnRenderProcessThreadCreated(
CefRefPtr<ClientAppBrowser> app,
CefRefPtr<CefListValue> extra_info) {}
};
typedef std::set<CefRefPtr<Delegate> > DelegateSet;

ClientAppBrowser();

private:
// CefApp methods.

void OnBeforeCommandLineProcessing(const CefString& process_type, CefRefPtr<CefCommandLine> command_line) OVERRIDE;
void OnRegisterCustomSchemes(CefRawPtr<CefSchemeRegistrar> registrar) OVERRIDE;
CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() OVERRIDE{return this;}

// CefBrowserProcessHandler methods.
void OnContextInitialized() OVERRIDE;
void OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> command_line) OVERRIDE;
void OnRenderProcessThreadCreated(CefRefPtr<CefListValue> extra_info) OVERRIDE;

private:
DelegateSet m_delegates;

IMPLEMENT_REFCOUNTING(ClientAppBrowser);
DISALLOW_COPY_AND_ASSIGN(ClientAppBrowser);
};
}

...全文
173 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 版主 2017-11-13
  • 打赏
  • 举报
回复
1、要不要用虚函数根据你的需要,也不是不用就一定不行。是调用哪个类的OnContextInitialized取决于调用时使用的指针的类型,如果是直接用对象调用,那和不使用虚函数时一样。 2、如果多重继承时多个父类有相同的虚函数,用原来的方式调用编译时会报错,需要显示调用,如:obj.A::fun() 3、那是纯虚函数,纯虚函数可以不用实现,但有这样函数的类也是抽象类,抽象类不能创建对象,纯虚函数具体用法可去网上搜索

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧